Skip to content
This repository has been archived by the owner on Mar 2, 2024. It is now read-only.

Commit

Permalink
Add support for keyword attributes
Browse files Browse the repository at this point in the history
This doesn't work exactly because of pyo3 get/set breaks this.
See PyO3/pyo3#743
  • Loading branch information
ethanhs committed Jan 23, 2020
1 parent 6638a23 commit b70277a
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 6 deletions.
66 changes: 64 additions & 2 deletions abserde/gen_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,61 @@
CONTAINER_TYPE_MAP = {"List": "Vec", "Optional": "Option"}


RUST_KEYWORDS = [
'abstract',
'become',
'box',
'do',
'final',
'macro',
'override',
'priv',
'typeof',
'unsized',
'virtual',
'yield',
'try',
'as',
'break',
'const',
'continue',
'crate',
'else',
'enum',
'extern',
'false',
'fn',
'for',
'if',
'impl',
'in',
'let',
'loop',
'match',
'mod',
'move',
'mut',
'pub',
'ref',
'return',
'self',
'Self',
'static',
'struct',
'super',
'trait',
'true',
'type',
'unsafe',
'use',
'where',
'while',
'async',
'await',
'dyn',
]


class InvalidTypeError(Exception):
pass

Expand Down Expand Up @@ -412,6 +467,12 @@ def _convert_simple(self, typ: str) -> str:
except KeyError:
invalid_type(typ)

def escape_keywords(self, name):
if name in RUST_KEYWORDS:
return f'r#{name}'
else:
return name

def is_union(self, item: AST) -> bool:
return isinstance(item.annotation, Subscript) and item.annotation.value.id == "Union"

Expand Down Expand Up @@ -490,9 +551,10 @@ def visit_ClassDef(self, n: ClassDef) -> None:
enums.append((annotation, members))
else:
annotation = self.convert(item.annotation)
attributes.append((name, annotation))
assert annotation is not None, print(n.name, item.target.id)
attributes.append((self.escape_keywords(name), annotation))
self.writeline(" " * 4 + "#[pyo3(get, set)]")
self.writeline(" " * 4 + f"pub {name}: {annotation},")
self.writeline(" " * 4 + f"pub {self.escape_keywords(name)}: {annotation},")
self.writeline("}")
# Then we write out the class implementation.
self.write(PYCLASS_PREFIX.format(name=n.name))
Expand Down
8 changes: 4 additions & 4 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ deps =
[testenv:py36]
commands = python -m pytest {posargs}
commands_pre =
python -m pip install --no-index --find-links=dist multiclass -U
python -m pip install --no-index --find-links=dist multiclass twitter -U


[testenv:py37]
Expand All @@ -27,7 +27,7 @@ setenv =
PYTHONWARNINGS=d
commands = python -m pytest {posargs}
commands_pre =
python -m pip install --no-index --find-links=dist multiclass -U
python -m pip install --no-index --find-links=dist multiclass twitter -U

[testenv:py38]
# Python 3.6+ has a number of compile-time warnings on invalid string escapes.
Expand All @@ -37,7 +37,7 @@ setenv =
PYTHONWARNINGS=d
commands = python -m pytest {posargs}
commands_pre =
python -m pip install --no-index --find-links=dist multiclass -U
python -m pip install --no-index --find-links=dist multiclass twitter -U

[testenv:lint]
basepython = python3.7
Expand All @@ -51,4 +51,4 @@ commands = pre-commit run --all-files

[flake8]
max-line-length = 99
ignore = F841
ignore = F841, W503

0 comments on commit b70277a

Please sign in to comment.