python / mypy

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

mypy crashes on importing littlefs (from littlefs-python-0.10.0) #17059

Open Krakonos opened 3 months ago

Krakonos commented 3 months ago

Crash Report

I have a library, littlefs-python. Using it crashes mypy. A simple example with mypy-1.9.0 and littlefs-python-0.10.0:

#!/usr/bin/python

import littlefs

This seems to the offending definition:

class LFSStat(NamedTuple):
    type: int
    size: int
    name: str

    # Constants
    TYPE_REG: int = LFS_TYPE_REG
    TYPE_DIR: int = LFS_TYPE_DIR

And changing LFS_TYPE_REG & LFS_TYPE_DIR to an integer constant fixes the issue. It likely is a problem of the stub and I will report a separate bug to littlefs-python upstream, however, IMHO mypy should report this as a problem, not crash.

Traceback

Traceback (most recent call last):
  File "/usr/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
             ^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/usr/lib/python3.11/site-packages/mypy/main.py", line 100, in main
    res, messages, blockers = run_build(sources, options, fscache, t0, stdout, stderr)
                              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/main.py", line 182, in run_build
    res = build.build(sources, options, None, flush_errors, fscache, stdout, stderr)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 192, in build
    result = _build(
             ^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 266, in _build
    graph = dispatch(sources, manager, stdout)
            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 2942, in dispatch
    process_graph(graph, manager)
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 3340, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 3467, in process_stale_scc
    graph[id].write_cache()
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 2503, in write_cache
    new_interface_hash, self.meta = write_cache(
                                    ^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/build.py", line 1564, in write_cache
    data = tree.serialize()
           ^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 387, in serialize
    "names": self.names.serialize(self._fullname),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 3933, in serialize
    data[key] = value.serialize(fullname, key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 3870, in serialize
    data["node"] = self.node.serialize()
                   ^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 3278, in serialize
    "names": self.names.serialize(self.fullname),
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 3933, in serialize
    data[key] = value.serialize(fullname, key)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/lib/python3.11/site-packages/mypy/nodes.py", line 3865, in serialize
    assert not isinstance(
AssertionError: Definition of littlefs.lfs.LFSStat.TYPE_REG is unexpectedly incomplete

To Reproduce

See snippet above.

Your Environment

ethanhs commented 3 months ago

Littlefs seems to be a C library, could you link to what stubs you are using, I don't see any in the project github.

hauntsaninja commented 3 months ago

I haven't yet tried to repro, but stub should be: https://github.com/jrast/littlefs-python/blob/fa538bded158f26530ee559d56b78a1273c8766f/src/littlefs/lfs.pyi#L9

Krakonos commented 3 months ago

Correct, this is the stub. Note that the version 0.10.1 fixes this stub, so use 0.10.0 for reproduction.