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

Crash in TypeVarType.serialize() when extracting dict().get as a variable #2804

Closed
gvanrossum opened this issue Feb 3, 2017 · 1 comment
Closed

Comments

@gvanrossum
Copy link
Member

Repro:

from typing import *
d = {}  # type: Dict[str, int]
g = d.get

Traceback:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/runpy.py", line 193, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/local/lib/python3.6/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/Users/guido/src/mypy/mypy/__main__.py", line 5, in <module>
    main(None)
  File "/Users/guido/src/mypy/mypy/main.py", line 42, in main
    res = type_check_only(sources, bin_dir, options)
  File "/Users/guido/src/mypy/mypy/main.py", line 86, in type_check_only
    options=options)
  File "/Users/guido/src/mypy/mypy/build.py", line 183, in build
    dispatch(sources, manager)
  File "/Users/guido/src/mypy/mypy/build.py", line 1531, in dispatch
    process_graph(graph, manager)
  File "/Users/guido/src/mypy/mypy/build.py", line 1768, in process_graph
    process_stale_scc(graph, scc)
  File "/Users/guido/src/mypy/mypy/build.py", line 1856, in process_stale_scc
    graph[id].write_cache()
  File "/Users/guido/src/mypy/mypy/build.py", line 1512, in write_cache
    self.manager)
  File "/Users/guido/src/mypy/mypy/build.py", line 862, in write_cache
    data = tree.serialize()
  File "/Users/guido/src/mypy/mypy/nodes.py", line 271, in serialize
    'names': self.names.serialize(self._fullname),
  File "/Users/guido/src/mypy/mypy/nodes.py", line 2265, in serialize
    data[key] = value.serialize(fullname, key)
  File "/Users/guido/src/mypy/mypy/nodes.py", line 2211, in serialize
    data['node'] = self.node.serialize()
  File "/Users/guido/src/mypy/mypy/nodes.py", line 671, in serialize
    'type': None if self.type is None else self.type.serialize(),
  File "/Users/guido/src/mypy/mypy/types.py", line 837, in serialize
    'items': [t.serialize() for t in self.items()],
  File "/Users/guido/src/mypy/mypy/types.py", line 837, in <listcomp>
    'items': [t.serialize() for t in self.items()],
  File "/Users/guido/src/mypy/mypy/types.py", line 763, in serialize
    for t in self.arg_types],
  File "/Users/guido/src/mypy/mypy/types.py", line 763, in <listcomp>
    for t in self.arg_types],
  File "/Users/guido/src/mypy/mypy/types.py", line 1079, in serialize
    'items': [t.serialize() for t in self.items],
  File "/Users/guido/src/mypy/mypy/types.py", line 1079, in <listcomp>
    'items': [t.serialize() for t in self.items],
  File "/Users/guido/src/mypy/mypy/types.py", line 496, in serialize
    assert not self.id.is_meta_var()
AssertionError
@ilevkivskyi
Copy link
Member

@gvanrossum
I think I have a fix for the problem #2809
The problem is caused by accessing generic methods:

from typing import TypeVar
T = TypeVar('T')

class D:
    def m(self, x: T) -> T:
        return x

g = D().m  # This crashes.

There are more details in the PR description.

JukkaL pushed a commit that referenced this issue Feb 7, 2017
Fixes #2804

The problem is that analyze_member_access calls freshen_function_type_vars 
if the member is a function (i.e. method). This is necessary to not mix the type 
variables during type inference of generic methods inside generic functions. 
However, if the method does not participate in type inference, type variables 
are left in meta_level = 1 state. This causes the error, since the types could not 
be serialized in the middle of type inference.

In this PR I propose to restore meta_level = 0 on member access, this is safe, 
since anyway, check_call always calls freshen_function_type_vars on callee (for 
generic functions, generic methods, etc).
JelleZijlstra added a commit to JelleZijlstra/mypy that referenced this issue Feb 11, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants