Open nndii opened 5 years ago
Needs investigation.
@nndii yes, that a bug indeed. And a very complex one. Oh man, I hate this rule. It is my personal nightmare.
The problem is with ast
parsing of tuple
in python
:
print(( # should start from here
1, 2, 3, # actually starts from here
))
and
print([
('Allow', 'group:editors', ('edit', 'create')), # tuple starts from here
])
ast
incorrectly detects lineno
. But, I am almost completely lost. How should it work?!
Maybe we can treat it as a single line? And just force all elements of these tuples to be on line with print
?
Like astor
does:
[('Allow', 'group:editors', ('edit', 'create'))]
('Allow', 'group:editors', ('edit', 'create'))
@nndii ok, here's what's wrong. ast
parsing of tuple
s is broken. And it can not be fixed right now.
You will have to live with the current implementation for some time.
Later, I will rewrite this check from ast
to tokenize
(I have spent almost a whole working week the last time I have tried).
Any help is highly appreciated. If you can handle this task - it would be the most significant contribution to the project ever.
@sobolevn Could you tell a bit how it's broken and why it's hard to fix?
@kxepal here's what's wrong:
ast
is abstract syntax tree. It means that some syntax is changed or droppedtuple
stuple
s are defined their line numbers are not preserved, see some examples abovetokenize
which preserves line numbersWhy is it hard?
token
s that can be nested into multiple levels, eg: [[1, [2],]]
Another false positive example:
class Meta:
model = User
fields = (
'id', 'username', 'first_name', 'last_name', 'email', 'created', 'modified', 'version',
'address', 'city', 'state', 'zip', 'groups',
)
@ffedoroff this is not false positive. Correct one should be:
class Meta(object):
model = User
fields = (
'id',
'username',
'first_name',
'last_name',
...
)
Related: https://github.com/jsfehler/flake8-multiline-containers/issues/9#issuecomment-519936744
I guess, that we can switch to flake8-multiline-containers
when this feature will be ready.
Bug report
What's wrong
Z317 enforces me to split nested tuples in multiple lines.
Code sample that causes that issue
How is that should be
There is "special case" in docs but its not actually clear whether I should follow it strictly or nested data types in one line is also allowed.
flake8 information