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

AssertionError with nested TypeVar #14137

Closed
cdce8p opened this issue Nov 19, 2022 · 2 comments · Fixed by #14148
Closed

AssertionError with nested TypeVar #14137

cdce8p opened this issue Nov 19, 2022 · 2 comments · Fixed by #14148
Labels

Comments

@cdce8p
Copy link
Collaborator

cdce8p commented Nov 19, 2022

Crash Report

Encountered this issue while testing the current master branch with Home Assistant (with all requirements installed).
Bisected it to #14095.

/CC: @ilevkivskyi

Traceback

  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 553, in serialize
    assert not self.id.is_meta_var()
AssertionError
Full traceback
  File "/home/runner/work/ha-core/ha-core/venv/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/main.py", line 95, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/main.py", line 174, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 193, in build
    result = _build(
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 276, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 2900, in dispatch
    process_graph(graph, manager)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 3288, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 3411, in process_stale_scc
    graph[id].write_cache()
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 2481, in write_cache
    new_interface_hash, self.meta = write_cache(
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/build.py", line 1571, in write_cache
    data = tree.serialize()
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/nodes.py", line 379, in serialize
    "names": self.names.serialize(self._fullname),
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/nodes.py", line 3779, in serialize
    data[key] = value.serialize(fullname, key)
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/nodes.py", line 3716, in serialize
    data["node"] = self.node.serialize()
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/nodes.py", line 1034, in serialize
    "type": None if self.type is None else self.type.serialize(),
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 1322, in serialize
    data["args"] = [arg.serialize() for arg in self.args]
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 1322, in <listcomp>
    data["args"] = [arg.serialize() for arg in self.args]
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 2016, in serialize
    "arg_types": [t.serialize() for t in self.arg_types],
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 2016, in <listcomp>
    "arg_types": [t.serialize() for t in self.arg_types],
  File "/home/runner/work/ha-core/ha-core/venv/lib/python3.9/site-packages/mypy/types.py", line 553, in serialize
    assert not self.id.is_meta_var()
AssertionError

I did add some debugging calls before the AssertionError. The serialization for this object failed

{
  '.class': 'TypeVarType', 'name': '_ZhaEntityT',
  'fullname': 'homeassistant.components.zha.core.registries._ZhaEntityT',
  'id': 13429, 'namespace': '', 'values': [], 'upper_bound': {
    '.class': 'TypeType', 'item': 'homeassistant.components.zha.entity.ZhaEntity'
  }, 'variance': 0
}

To Reproduce

Unfortunately I wasn't yet able to narrow it further to a short code sample.

However, it is easily reproducible with Home Assistant

  • Check out Home Assistant
  • Install attrs==21.2.0 or types-attrs==19.1.0 (only dependency required to reproduce the error it
  • Run mypy homeassistant/components/zha/core

Your Environment

  • Mypy version used: mypy 1.0.0+dev.a2477ff0d0cb751f27a2b38d27ce6572ead03451
  • Mypy configuration options from mypy.ini (and other config files): see Home Assistant repo
  • Python version used: 3.9
  • Operating system and version: Github actions -> Ubuntu-20.04
@cdce8p cdce8p added the crash label Nov 19, 2022
@cdce8p cdce8p changed the title AssertionError with TypeVar AssertionError with TypeVar and attrs Nov 19, 2022
@ilevkivskyi
Copy link
Member

It looks like attrs is not important here, a simple repro for the crash:

[case testIncrementalNestedGenericCallableCrash]
from typing import TypeVar, Callable

T = TypeVar("T")

class B:
    def foo(self) -> Callable[[T], T]: ...

class C(B):
    def __init__(self) -> None:
        self.x = self.foo()
[out]
[out2]

@cdce8p cdce8p changed the title AssertionError with TypeVar and attrs AssertionError with nested TypeVar Nov 19, 2022
ilevkivskyi added a commit that referenced this issue Nov 19, 2022
#14148)

Fixes #14137 

Fix is trivial, I just forgot to call `super()` in one of my previous
PRs.
@cdce8p
Copy link
Collaborator Author

cdce8p commented Nov 20, 2022

Can confirm #14148 does resolve the issue. Thanks @ilevkivskyi for the quick fix!

ilevkivskyi pushed a commit that referenced this issue Dec 19, 2022
Currently, `mypy_primer` sets `--cache-dir=/dev/null` which disables
cache generation. This can result in errors being missed which would
normally come up during `tree.serialize()`. Removing
`--cache-dir=/dev/null` isn't practical.

This PR adds a new debug / test option `--debug-serialize` which runs
`tree.serialize()` even if cache generation is disabled to help detect
serialize errors earlier.

**Refs**
* #14137
*
hauntsaninja/mypy_primer#54 (review)

cc: @hauntsaninja
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.

2 participants