Skip to content

Internal error with generic type and alias using import * #2315

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

Closed
refi64 opened this issue Oct 25, 2016 · 4 comments · Fixed by #2445
Closed

Internal error with generic type and alias using import * #2315

refi64 opened this issue Oct 25, 2016 · 4 comments · Fixed by #2445
Labels

Comments

@refi64
Copy link
Contributor

refi64 commented Oct 25, 2016

Dang, I'm on a bug roll today. :D

In x.py:

from y import *

notes = None  # type: G[X]
# Using the new annotation syntax `notes: G[X]` can also reproduce the crash.

y.py:

from typing import Generic, TypeVar

T = TypeVar('T')

class G(Generic[T]):
    pass

X = G[int]

Output:

ryan@DevPC-LX ~/blaze-mypy/bug master $ mypy x.py --show-traceback
x.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
  File "/media/ryan/stuff/anaconda/bin/mypy", line 6, in <module>
    exec(compile(open(__file__).read(), __file__, 'exec'))
  File "/media/ryan/stuff/mypy/scripts/mypy", line 6, in <module>
    main(__file__)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/main.py", line 38, in main
    res = type_check_only(sources, bin_dir, options)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/main.py", line 79, in type_check_only
    options=options)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/build.py", line 184, in build
    dispatch(sources, manager)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/build.py", line 1494, in dispatch
    process_graph(graph, manager)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/build.py", line 1674, in process_graph
    process_stale_scc(graph, scc)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/build.py", line 1751, in process_stale_scc
    graph[id].semantic_analysis_pass_three()
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/build.py", line 1411, in semantic_analysis_pass_three
    self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/semanal.py", line 2871, in visit_file
    self.accept(file_node)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/semanal.py", line 2875, in accept
    node.accept(self)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/nodes.py", line 259, in accept
    return visitor.visit_mypy_file(self)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/traverser.py", line 29, in visit_mypy_file
    d.accept(self)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/nodes.py", line 815, in accept
    return visitor.visit_assignment_stmt(self)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/semanal.py", line 2952, in visit_assignment_stmt
    self.analyze(s.type)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/semanal.py", line 2972, in analyze
    type.accept(analyzer)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/types.py", line 427, in accept
    return visitor.visit_instance(self)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/typeanal.py", line 363, in visit_instance
    if not satisfies_upper_bound(arg, TypeVar.upper_bound):
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/subtypes.py", line 75, in satisfies_upper_bound
    return isinstance(a, Void) or is_subtype(a, upper_bound)
  File "/media/stuff/anaconda/lib/python3.4/site-packages/mypy/subtypes.py", line 51, in is_subtype
    return left.accept(SubtypeVisitor(right, type_parameter_checker))
AttributeError: 'NoneType' object has no attribute 'accept'
x.py:1: note: use --pdb to drop into pdb
ryan@DevPC-LX ~/blaze-mypy/bug master $ 
@gvanrossum
Copy link
Member

Reproduced.

@gvanrossum gvanrossum added this to the 0.4.x milestone Oct 25, 2016
@davire
Copy link

davire commented Nov 8, 2016

Another example that raises the issue without generic type:

file ex2a.py

from typing import Dict

Row = Dict[str, int]

file ex2.py

from ex2a import *

def do_something(dic: Row) -> None:
    pass

results in :

mypy --show-traceback ex2.py 
ex2.py:1: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 6, in <module>
    main(__file__)
  File "/usr/local/lib/python3.5/site-packages/mypy/main.py", line 38, in main
    res = type_check_only(sources, bin_dir, options)
  File "/usr/local/lib/python3.5/site-packages/mypy/main.py", line 79, in type_check_only
    options=options)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 181, in build
    dispatch(sources, manager)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1470, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1650, in process_graph
    process_stale_scc(graph, scc)
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1723, in process_stale_scc
    graph[id].semantic_analysis_pass_three()
  File "/usr/local/lib/python3.5/site-packages/mypy/build.py", line 1406, in semantic_analysis_pass_three
    self.manager.semantic_analyzer_pass3.visit_file(self.tree, self.xpath, self.options)
  File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 2770, in visit_file
    self.accept(file_node)
  File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 2774, in accept
    node.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 216, in accept
    return visitor.visit_mypy_file(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/traverser.py", line 29, in visit_mypy_file
    d.accept(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/nodes.py", line 507, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 2785, in visit_func_def
    self.analyze(fdef.type)
  File "/usr/local/lib/python3.5/site-packages/mypy/semanal.py", line 2871, in analyze
    type.accept(analyzer)
  File "/usr/local/lib/python3.5/site-packages/mypy/types.py", line 639, in accept
    return visitor.visit_callable_type(self)
  File "/usr/local/lib/python3.5/site-packages/mypy/typeanal.py", line 381, in visit_callable_type
    arg_type.accept(self)
AttributeError: 'NoneType' object has no attribute 'accept'
ex2.py:1: note: use --pdb to drop into pdb

@davire
Copy link

davire commented Nov 8, 2016

Note that in the previous example, if you replace from ex2a import * with from ex2a import Row, the internal error disappears.

@gvanrossum
Copy link
Member

I'm thinking the issue has to do with "import *" mistreating certain types and aliases.

@gvanrossum gvanrossum changed the title Internal error with generic type and alias across modules Internal error with generic type and alias using import * Nov 8, 2016
gvanrossum pushed a commit that referenced this issue Nov 14, 2016
Fixes #2315
Fixes #2172

There were reported some crashes of mypy when using import * and type aliases (only two above issues seem to be still open, others are closed as duplicates).

It looks like this is because visit_import_all in semanal.py does not copy type_override for imported nodes (visit_import_from does, so that the crashes reported happen only with import *).

I added few tests based on (extended) examples discussed in bug reports. Since the reason is clear and the fix is straightforward (one line) I think more sophisticated tests are not required.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants