python / mypy

Optional static typing for Python
https://www.mypy-lang.org/
Other
18.44k stars 2.82k forks source link

"AttributeError: 'int' object has no attribute 'is_meta_var'" in 1.11 #17516

Closed Redoubts closed 3 months ago

Redoubts commented 3 months ago

Bug Report

I pip-installed the 1.11 branch to take a preview of the next release, and I saw a crash when running mypy:

pip install -U git+https://github.com/python/mypy.git@release-1.11

Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "<my-venv>/lib/python3.11/site-packages/mypy/__main__.py", line 37, in <module>
    console_entry()
  File "<my-venv>/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "<my-venv>/lib/python3.11/site-packages/mypy/main.py", line 103, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/main.py", line 187, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 193, in build
    result = _build(
             ^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 268, in _build
    graph = dispatch(sources, manager, stdout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 2950, in dispatch
    process_graph(graph, manager)
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 3348, in process_graph
    process_stale_scc(graph, scc, manager)
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 3475, in process_stale_scc
    graph[id].write_cache()
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 2507, in write_cache
    new_interface_hash, self.meta = write_cache(
                                    ^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/build.py", line 1568, in write_cache
    data = tree.serialize()
           ^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 390, in serialize
    "names": self.names.serialize(self._fullname),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 4012, in serialize
    data[key] = value.serialize(fullname, key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 3949, in serialize
    data["node"] = self.node.serialize()
                   ^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 3351, in serialize
    "names": self.names.serialize(self.fullname),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 4012, in serialize
    data[key] = value.serialize(fullname, key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 3949, in serialize
    data["node"] = self.node.serialize()
                   ^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 935, in serialize
    "var": self.var.serialize(),
           ^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/nodes.py", line 1081, in serialize
    "type": None if self.type is None else self.type.serialize(),
                                           ^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2335, in serialize
    return {".class": "Overloaded", "items": [t.serialize() for t in self.items]}
                                             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2335, in <listcomp>
    return {".class": "Overloaded", "items": [t.serialize() for t in self.items]}
                                              ^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2223, in serialize
    "arg_types": [t.serialize() for t in self.arg_types],
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2223, in <listcomp>
    "arg_types": [t.serialize() for t in self.arg_types],
                  ^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2223, in serialize
    "arg_types": [t.serialize() for t in self.arg_types],
                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 2223, in <listcomp>
    "arg_types": [t.serialize() for t in self.arg_types],
                  ^^^^^^^^^^^^^
  File "<my-venv>/lib/python3.11/site-packages/mypy/types.py", line 663, in serialize
    assert not self.id.is_meta_var()
               ^^^^^^^^^^^^^^^^^^^
AttributeError: 'int' object has no attribute 'is_meta_var'

To Reproduce

Hard to share, this happens when running on my company code. Can look more closely by request, if the traceback isn't enough.

Expected Behavior

No traceback from mypy

Actual Behavior

(See traceback)

Your Environment

sobolevn commented 3 months ago

Reproducer is needed 🙏

hauntsaninja commented 3 months ago

That's strange and concerning

cdce8p commented 3 months ago

This looks like it's related to an overload with a TypeVar. Do you use custom mypy plugins? Maybe the one for pydantic? The TypeVar.id was changed this release cycle to only accept TypeVarId types, no longer ints. If a plugin isn't updated, that might cause the issue / crash. However, to say for sure, we need a reproducer.

For pydantic v1: make sure to run at least >=1.10.16 as that's the "fixed" version. https://github.com/pydantic/pydantic/releases/tag/v1.10.16

Redoubts commented 3 months ago

It's definitely a plugin. Looks like this will stop crashing if I turn off trio-typing. I'll close this as this feels like a 3rd party problem, though I wonder if there's a way for mypy to better blame a plugin for things like this. I think I've been burned by a plugin that doesn't work well with a newer mypy before, and it always feels as inscrutable.

antoninkriz commented 3 months ago

@cdce8p Hello, shouldn't this change be reflected in the "Announcement issue for plugin API changes" #6617 issue? Currently a plugin I maintain fails after updating the mypy dependency since we use integers in the id parameter in mypy.types.TypeVarType(...). I guess the change won't be too difficult since we just use -1 as the int value, but it still breaks the plugin. Thank you!