Skip to content

Commit

Permalink
Add type annotations (#21), update project
Browse files Browse the repository at this point in the history
Simplify environment by using ruff
Don't assume there's a regex match (found by `mypy`)
  • Loading branch information
mwermelinger committed Jan 11, 2024
1 parent ed809ba commit 146b5fd
Show file tree
Hide file tree
Showing 4 changed files with 196 additions and 691 deletions.
12 changes: 3 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,12 @@ update:
poetry update

format:
poetry run autoflake \
--in-place \
--remove-all-unused-imports \
-r \
allowed.py
poetry run isort --profile black allowed.py
poetry run black allowed.py
poetry run ruff format allowed.py

lint:
poetry run bandit allowed.py
poetry run mypy --ignore-missing-imports allowed.py
poetry run flake8 allowed.py
poetry run bandit -q -r allowed.py
poetry run ruff check allowed.py

test:
poetry run ./tests.sh run
17 changes: 9 additions & 8 deletions allowed.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ def get_unit(filename: str) -> int:
return 0


def get_language(last_unit: int) -> tuple[ast.AST]:
def get_language(last_unit: int) -> tuple[type[ast.AST], ...]:
"""Return the allowed language elements up to the given unit.
If `last_unit` is zero, return the elements in all units.
Expand Down Expand Up @@ -319,7 +319,7 @@ def get_imports(last_unit: int) -> dict[str, list[str]]:
If `last_unit` is zero, return the imports in all units.
"""
allowed = {}
allowed: dict[str, list[str]] = {}
for unit, imports in IMPORTS.items():
if not last_unit or unit <= last_unit:
for module, names in imports.items():
Expand Down Expand Up @@ -349,7 +349,7 @@ def get_methods(last_unit: int) -> dict[str, list[str]]:
If `last_unit` is zero, return the methods in all units.
"""
allowed = {}
allowed: dict[str, list[str]] = {}
for unit, methods in METHODS.items():
if not last_unit or unit <= last_unit:
for type, names in methods.items():
Expand Down Expand Up @@ -447,11 +447,12 @@ def check_tree(
message = f"{name.id}.{attribute}"
errors.append((cell, line, message))
elif hasattr(name, "resolved_annotation"):
type_name = re.match(r"[a-zA-Z.]*", name.resolved_annotation).group()
if type_name in methods and attribute not in methods[type_name]:
cell, line = location(name.lineno, line_cell_map)
message = f"{type_name.lower()}.{attribute}()"
errors.append((cell, line, message))
if matched := re.match(r"[a-zA-Z.]*", name.resolved_annotation):
type_name = matched.group()
if type_name in methods and attribute not in methods[type_name]:
cell, line = location(name.lineno, line_cell_map)
message = f"{type_name.lower()}.{attribute}()"
errors.append((cell, line, message))
elif isinstance(node, ast.Call) and isinstance(node.func, ast.Name):
function = node.func.id
if function in BUILTINS and function not in functions:
Expand Down
Loading

0 comments on commit 146b5fd

Please sign in to comment.