-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Fix reporting location in aliased types. #12745
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
test-data/unit/check-generics.test
Outdated
reveal_type(a) # N: Revealed type is "other.array[Any, other.dtype[builtins.float]]" | ||
|
||
[out] | ||
main:2: error: Type argument "float" of "dtype" must be a subtype of "generic" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
output with the current master is
main:12: error: Type argument "float" of "dtype" must be a subtype of "generic" (diff)
where 12 is the line number in other.py but is used as a reference with main.py.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with --pretty
flag the test fails on master as
[gw0] linux -- Python 3.8.10 /home/miha/foss/mypy/venv/bin/python3
data: /home/miha/foss/mypy/test-data/unit/check-generics.test:651:
/home/miha/foss/mypy/mypy/test/testcheck.py:141: in run_case
self.run_case_once(testcase)
/home/miha/foss/mypy/mypy/test/testcheck.py:199: in run_case_once
res = build.build(sources=sources,
/home/miha/foss/mypy/mypy/build.py:181: in build
result = _build(
/home/miha/foss/mypy/mypy/build.py:257: in _build
graph = dispatch(sources, manager, stdout)
/home/miha/foss/mypy/mypy/build.py:2752: in dispatch
process_graph(graph, manager)
/home/miha/foss/mypy/mypy/build.py:3110: in process_graph
process_stale_scc(graph, scc, manager)
/home/miha/foss/mypy/mypy/build.py:3227: in process_stale_scc
manager.flush_errors(manager.errors.file_messages(graph[id].xpath), False)
/home/miha/foss/mypy/mypy/errors.py:707: in file_messages
return self.format_messages(self.error_info_map[path], source_lines)
/home/miha/foss/mypy/mypy/errors.py:680: in format_messages
source_line = source_lines[line - 1]
E IndexError: list index out of range
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
mypy/types.py
Outdated
tp.line = newline | ||
tp.column = newcolumn | ||
for ta in getattr(tp, 'args', []): | ||
replace_alias_location(ta, newline, newcolumn) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might not find all component types (e.g. within callables). Also, we prefer to avoid getattr
since it can't be type checked by mypy and mypyc generates slow code for it.
Can you instead define a subclass of TypeTraverserVisitor
, and override at least visit_instance
to update line/column info. You can use it to update all component type objects (of the desired types), and in a type-safe manner.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@JukkaL I've updated the PR, please could you check again?
This comment has been minimized.
This comment has been minimized.
@JukkaL any chance to get the fix merged? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A few additional minor things, otherwise looks good.
mypy/types.py
Outdated
new_tp.column = newcolumn | ||
return new_tp | ||
newtp = tp.accept(replacer) | ||
newtp.accept(LocationSetter(newline, newcolumn)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To make sure that we don't regress any existing use case, can you also set the line
and column
attributes of the return value of the visitor, in case line/column of non-Instance types is important.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated
@@ -2925,16 +2926,26 @@ def visit_type_var(self, typ: TypeVarType) -> Type: | |||
return typ | |||
|
|||
|
|||
class LocationSetter(TypeTraverserVisitor): | |||
def __init__(self, line: int, column: int) -> None: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a TODO comment along the lines of "Should we update locations of other Type subclasses?".
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
added
@JukkaL Thanks for your comments! I've updated PR as suggested. |
This comment has been minimized.
This comment has been minimized.
@JukkaL ping |
@oxidase There is now merge conflict, sorry, can't merge. Can you please rebase/merge? |
This comment has been minimized.
This comment has been minimized.
@JukkaL rebased to the current master |
According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉 |
@JukkaL sorry, I can not merge in the repository due to
Please could you merge ^ PR? |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the updates!
Description
Fixes #12678 by updating line and column not only in an alias type variable but also recursively in arguments.
The current develop output
The branch output
Test Plan
(Write your test plan here. If you changed any code, please provide us with clear instructions on how you verified your changes work.)