Skip to content
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

Forward References under attrs classes extending Generic breaks mypy #5864

Closed
tombo315 opened this issue Nov 1, 2018 · 2 comments
Closed
Labels
crash priority-1-normal topic-attrs topic-plugins The plugin API and ideas for new plugins

Comments

@tombo315
Copy link

tombo315 commented Nov 1, 2018

System information:

$ python3 --version
Python 3.7.0
$ mypy --version
mypy 0.650+dev.0b61e5476520c6e2b8d0136fab645cb3414e412f

Minimal[-ish] example:

# mypy_break.py
import typing
from typing import Generic, Mapping, Union

import attr

T = typing.TypeVar('T', str, int)

@attr.s
class Klass(Generic[T], object):
    modified = attr.ib(type=Mapping[T, 'UnionAlias'])


UnionAlias = Union[str, int]
$ mypy mypy_break.py --show-traceback
mypy_break.py:7: error: INTERNAL ERROR -- please report a bug at https://github.com/python/mypy/issues version: 0.650+dev.0b61e5476520c6e2b8d0136fab645cb3414e412f
Traceback (most recent call last):
  File "/usr/local/bin/mypy", line 11, in <module>
    sys.exit(console_entry())
  File "/usr/local/lib/python3.7/site-packages/mypy/__main__.py", line 7, in console_entry
    main(None)
  File "/usr/local/lib/python3.7/site-packages/mypy/main.py", line 92, in main
    res = build.build(sources, options, None, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 155, in build
    result = _build(sources, options, alt_lib_path, flush_errors, fscache)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 204, in _build
    graph = dispatch(sources, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2172, in dispatch
    process_graph(graph, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2469, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 2592, in process_stale_scc
    graph[id].type_check_first_pass()
  File "/usr/local/lib/python3.7/site-packages/mypy/build.py", line 1730, in type_check_first_pass
    self.type_checker().check_first_pass()
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 282, in check_first_pass
    self.accept(d)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 845, in accept
    return visitor.visit_class_def(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 1536, in visit_class_def
    self.accept(defn.defs)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 910, in accept
    return visitor.visit_block(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 1688, in visit_block
    self.accept(s)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 393, in accept
    stmt.accept(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 608, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 704, in visit_func_def
    self._visit_func_def(defn)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 708, in _visit_func_def
    self.check_func_item(defn, name=defn.name())
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 770, in check_func_item
    self.check_func_def(defn, typ, name)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 787, in check_func_def
    for item, typ in self.expand_typevars(defn, typ):
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 1280, in expand_typevars
    result.append((expand_func(defn, mapping), expanded))
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 3931, in expand_func
    ret = defn.accept(visitor)
  File "/usr/local/lib/python3.7/site-packages/mypy/nodes.py", line 608, in accept
    return visitor.visit_func_def(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/treetransform.py", line 106, in visit_func_def
    [self.copy_argument(arg) for arg in node.arguments],
  File "/usr/local/lib/python3.7/site-packages/mypy/treetransform.py", line 106, in <listcomp>
    [self.copy_argument(arg) for arg in node.arguments],
  File "/usr/local/lib/python3.7/site-packages/mypy/treetransform.py", line 80, in copy_argument
    self.visit_var(argument.variable),
  File "/usr/local/lib/python3.7/site-packages/mypy/treetransform.py", line 199, in visit_var
    new = Var(node.name(), self.optional_type(node.type))
  File "/usr/local/lib/python3.7/site-packages/mypy/treetransform.py", line 584, in optional_type
    return self.type(type)
  File "/usr/local/lib/python3.7/site-packages/mypy/checker.py", line 3942, in type
    return expand_type(type, self.map)
  File "/usr/local/lib/python3.7/site-packages/mypy/expandtype.py", line 16, in expand_type
    return typ.accept(ExpandTypeVisitor(env))
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 524, in accept
    return visitor.visit_instance(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/expandtype.py", line 83, in visit_instance
    args = self.expand_types(t.args)
  File "/usr/local/lib/python3.7/site-packages/mypy/expandtype.py", line 132, in expand_types
    a.append(t.accept(self))
  File "/usr/local/lib/python3.7/site-packages/mypy/types.py", line 1532, in accept
    return visitor.visit_forwardref_type(self)
  File "/usr/local/lib/python3.7/site-packages/mypy/type_visitor.py", line 101, in visit_forwardref_type
    raise RuntimeError('Internal error: unresolved forward reference')
RuntimeError: Internal error: unresolved forward reference
mypy_break.py:7: : note: use --pdb to drop into pdb
@tombo315 tombo315 changed the title Mapping[TypeVar, Union] under attrs breaks mypy Forward References under attrs classes extending Generic breaks mypy Nov 1, 2018
@ilevkivskyi
Copy link
Member

Any of these two should solve the problem:

  • Move the union definition before the class
  • Use an annotation for modified, instead of passing the type as type=...

Ideally, this should be supported, but at least mypy should not crash.
@euresti might help with this.

@97littleleaf11
Copy link
Collaborator

This issue cannot be reproduced in latest branch. It seems fine to close it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash priority-1-normal topic-attrs topic-plugins The plugin API and ideas for new plugins
Projects
None yet
Development

No branches or pull requests

4 participants