best-doctor / flake8-annotations-complexity Goto Github PK
View Code? Open in Web Editor NEWflake8 plugin to validate annotations complexity
License: MIT License
flake8 plugin to validate annotations complexity
License: MIT License
Hi, thank you for this awesome plugin! Please consider creatng a new release that contains the changes introduced in the PR #14. Otherwise, the latest release on PyPI (0.0.5) raises on Python 3.9, forcing us to resort to install from source repo.
Привет, ребята! Спасибо за отличный плагин к flake8
! К сожалению, текущая версия на PyPI (0.0.5) кидает исключение в связке с Python 3.9 - фикс уже есть в основной ветке (PR #14). Прошу выпустить новую версию 0.0.6, содержащую этот фикс - пока приходится устанавливать плагин с мастер-ветки репозитория.
I have some functions that deal with unions where one variant is the empty tuple. Per python/mypy#4211, the way to represent the empty tuple type is typing.Tuple[()]
. Using this annotation crashes flake8-annotations-complexity with the following traceback:
Traceback (most recent call last):
File "/Users/maxchase/.pyenv/versions/structured-data/bin/flake8", line 10, in <module>
sys.exit(main())
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/main/application.py", line 381, in _run
self.run_checks()
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/main/application.py", line 300, in run_checks
self.file_checker_manager.run()
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/checker.py", line 331, in run
self.run_serial()
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/checker.py", line 315, in run_serial
checker.run_checks()
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8/checker.py", line 502, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/checker.py", line 25, in run
self.max_annotations_complexity,
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 28, in validate_annotations_in_ast_node
complexity = get_annotation_compexity(annotation)
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 9, in get_annotation_compexity
return 1 + get_annotation_compexity(annotation_node.slice.value) # type: ignore
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 11, in get_annotation_compexity
return max(get_annotation_compexity(n) for n in annotation_node.elts)
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 11, in <genexpr>
return max(get_annotation_compexity(n) for n in annotation_node.elts)
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 9, in get_annotation_compexity
return 1 + get_annotation_compexity(annotation_node.slice.value) # type: ignore
File "/Users/maxchase/.pyenv/versions/3.7.4/envs/structured-data/lib/python3.7/site-packages/flake8_annotations_complexity/ast_helpres.py", line 11, in get_annotation_compexity
return max(get_annotation_compexity(n) for n in annotation_node.elts)
ValueError: max() arg is an empty sequence
I can put in a PR for this.
This is not nested, but also not okey: Tuple[str, str, str, int, List, Any, str, Dict, int]
.
This is ok: List[str]
, this is not ok: List['str']
, this is also not ok: 'List[str]'
.
According PEP 3107 we can use any expression in annotations. Annotations like this:
def foo() -> 'String Annontation':
pass
raises syntax error due to impossibility AST parsing this string:
Traceback (most recent call last):
File "/Users/andrey/.envs/test/bin/flake8", line 8, in <module>
sys.exit(main())
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/main/application.py", line 381, in _run
self.run_checks()
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/main/application.py", line 300, in run_checks
self.file_checker_manager.run()
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/checker.py", line 331, in run
self.run_serial()
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/checker.py", line 315, in run_serial
checker.run_checks()
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8/checker.py", line 502, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8_annotations_complexity/checker.py", line 23, in run
too_difficult_annotations = validate_annotations_in_ast_node(
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 28, in validate_annotations_in_ast_node
complexity = get_annotation_compexity(annotation)
File "/Users/andrey/.envs/test/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 7, in get_annotation_compexity
annotation_node = ast.parse(annotation_node.s).body[0].value # type: ignore
File "/Users/andrey/.pyenv/versions/3.8.0/lib/python3.8/ast.py", line 47, in parse
return compile(source, filename, mode, flags,
File "<unknown>", line 1
String Annontation
^
SyntaxError: invalid syntax
File "/Users/ilebedev/.virtualenvs/amy/bin/flake8", line 8, in <module>
sys.exit(main())
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/main/cli.py", line 18, in main
app.run(argv)
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/main/application.py", line 393, in run
self._run(argv)
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/main/application.py", line 381, in _run
self.run_checks()
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/main/application.py", line 300, in run_checks
self.file_checker_manager.run()
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/checker.py", line 331, in run
self.run_serial()
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/checker.py", line 315, in run_serial
checker.run_checks()
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8/checker.py", line 502, in run_ast_checks
for (line_number, offset, text, check) in runner:
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8_annotations_complexity/checker.py", line 23, in run
too_difficult_annotations = validate_annotations_in_ast_node(
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 28, in validate_annotations_in_ast_node
complexity = get_annotation_compexity(annotation)
File "/Users/ilebedev/.virtualenvs/amy/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 9, in get_annotation_compexity
return 1 + get_annotation_compexity(annotation_node.slice.value) # type: ignore
AttributeError: 'Slice' object has no attribute 'value'
Code:
PROJECTS_INFO: Final[List[Tuple[int, str, str, str]]] = [
]
annotation_len compares with constant 7 and not max_annotation_len :(
But max_annotation_len is used in error message formatting.
Is your feature request related to a problem? Please describe.
The only documented method for configuring the max complexity is a command line switch. It is difficult to enforce a common standard in a shared codebase, or ensure CI is using the correct settings this way.
Describe the solution you'd like
A configuration option I can put in pyproject.toml
to ensure the setting I desire is respected for all callers of flake8
in my repo.
Describe alternatives you've considered
Do nothing. Easiest, but it doesn't address the issue.
Additional context
N/A
An exception is thrown if running flake8 and it finds an empty string in a Literal definition.
To Reproduce
I wrote the test for it, so see that:
#11
Output:
Traceback (most recent call last):
File "/home/ubuntu/.local/bin/flake8", line 8, in <module>
sys.exit(main())
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/main/cli.py", line 22, in main
app.run(argv)
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/main/application.py", line 360, in run
self._run(argv)
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/main/application.py", line 348, in _run
self.run_checks()
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/main/application.py", line 262, in run_checks
self.file_checker_manager.run()
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/checker.py", line 334, in run
self.run_serial()
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/checker.py", line 318, in run_serial
checker.run_checks()
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/checker.py", line 598, in run_checks
self.run_ast_checks()
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8/checker.py", line 505, in run_ast_checks
for (line_number, offset, text, _) in runner:
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8_annotations_complexity/checker.py", line 23, in run
too_difficult_annotations = validate_annotations_in_ast_node(
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 31, in validate_annotations_in_ast_node
complexity = get_annotation_complexity(annotation)
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 12, in get_annotation_complexity
return 1 + get_annotation_complexity(annotation_node.slice.value) # type: ignore
File "/home/ubuntu/.local/lib/python3.8/site-packages/flake8_annotations_complexity/ast_helpres.py", line 8, in get_annotation_complexity
annotation_node = ast.parse(annotation_node.s).body[0].value # type: ignore
IndexError: list index out of range
Maybe current setup arguments are not best, that fits for the plugin.
Here you only check function annotations: https://github.com/best-doctor/flake8-annotations-complexity/blob/master/flake8_annotations_complexity/ast_helpres.py#L17-L20
However, it is still possible to use really complex variable annotations:
some_value: List[Union[List[str], Dict[str, Dict[str, str]]]] = ...
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.