Skip to content

"unresolved placeholder type None" on mypy 1.0.0 #14631

Closed

Description

@jonathanslenders

Bug Report

There is a bug that makes it impossible to typecheck any Python application/library that has an import of the asyncssh package. However, typechecking asyncssh directly doesn't result in this bug. (See below, for a sort of reproducer).

The error looks like this:

Traceback (most recent call last):
  File "/home/jonathan/.pyenv/versions/env/bin/mypy", line 8, in <module>
    sys.exit(console_entry())
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/__main__.py", line 15, in console_entry
    main()
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/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/jonathan/.pyenv/versions/3.9.13/envs/env/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/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 194, in build
    result = _build(
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 277, in _build
    graph = dispatch(sources, manager, stdout)
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 2923, in dispatch
    process_graph(graph, manager)
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 3320, in process_graph
    process_stale_scc(graph, scc, manager)
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 3443, in process_stale_scc
    graph[id].write_cache()
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 2504, in write_cache
    new_interface_hash, self.meta = write_cache(
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/build.py", line 1574, in write_cache
    data = tree.serialize()
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/nodes.py", line 377, in serialize
    "names": self.names.serialize(self._fullname),
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/nodes.py", line 3841, in serialize
    data[key] = value.serialize(fullname, key)
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/nodes.py", line 3778, in serialize
    data["node"] = self.node.serialize()
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/nodes.py", line 3514, in serialize
    "target": self.target.serialize(),
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/types.py", line 2072, in serialize
    "ret_type": self.ret_type.serialize(),
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/types.py", line 605, in serialize
    "values": [v.serialize() for v in self.values],
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/types.py", line 605, in <listcomp>
    "values": [v.serialize() for v in self.values],
  File "/home/jonathan/.pyenv/versions/3.9.13/envs/env/lib/python3.9/site-packages/mypy/types.py", line 2876, in serialize
    assert False, f"Internal error: unresolved placeholder type {self.fullname}"
AssertionError: Internal error: unresolved placeholder type None

To Reproduce

Install asyncssh, then create a Python file with only this import and typecheck that file.

import asyncssh

Or:

mypy -c 'import asyncssh'

I tried to reduce the code surface as much as possible. Removing every file within asyncssh, except py.typed and __init__.py, and then write the following in __init__.py:

from typing import TypeVar, Callable
T = TypeVar('T', 'A', 'B')
Func = Callable[[], T]

With the above snippet it still happens.

Your Environment

  • Mypy version used: 1.0.0
  • Mypy command-line flags: --strict
  • Mypy configuration options from mypy.ini (and other config files): /
  • Python version used: 3.9.13

cc: @ronf

Activity

  1. wjurkowlaniec commented on Feb 7, 2023

    @wjurkowlaniec

    I have the same problem..

  2. added and removed
    bugmypy got something wrong
    on Feb 7, 2023
  3. hauntsaninja commented on Feb 7, 2023

    @hauntsaninja
    Collaborator

    Thanks for the report! Confirming the crash and the regression from 0.991

    And thank you for the minimal repro here!

  4. added a commit that references this issue on Feb 8, 2023
    03981f3
  5. added a commit that references this issue on Feb 8, 2023
    786c7b0
  6. added 2 commits that reference this issue on Feb 8, 2023
    a7b124a
    0c99585
  7. jonathanslenders commented on Feb 10, 2023

    @jonathanslenders
    Author

    @hauntsaninja : Thanks for the quick fix!!

  8. ronf commented on Feb 12, 2023

    @ronf

    I just upgraded to mypy 1.0.0 here and I'm now getting an error running mypy on the AsyncSSH package with the definition of the _ConnectionFactory type that triggered this issue:

    async def _open_proxy(
            loop: asyncio.AbstractEventLoop, command: Sequence[str],
            conn_factory: _ConnectionFactory[_Conn]) -> _Conn:

    The relevant definitions are:

    _Conn = TypeVar('_Conn', 'SSHClientConnection', 'SSHServerConnection')
    _ConnectionFactory = Callable[[], _Conn]

    The SSHClientConnection and SSHServerConnection classes are declared later in this module.

    The specific error is:

    asyncssh/connection.py:272: error: Invalid type argument value for "_ConnectionFactory" [type-var]

    Any idea how I avoid this error? It only shows up in this one place, while other similar references to _ConnectionFactory[_Conn] are working fine. For instance, the following doesn't give an error:

    async def _connect(options: 'SSHConnectionOptions',
                       loop: asyncio.AbstractEventLoop, flags: int,
                       sock: Optional[socket.socket],
                       conn_factory: _ConnectionFactory[_Conn], msg: str) -> _Conn:

    My environment:

    • Mypy version used: 1.0.0
    • Mypy command-line flags: none
    • Mypy configuration options from mypy.ini (and other config files):
      • allow_redefinition = True
      • ignore_missing_imports = True
    • Python version used: 3.10.9
  9. Tasssadar commented on Mar 2, 2023

    @Tasssadar

    Hello, we are still seeing this error with mypy==1.0.1:

    $ mypy --version
    mypy 1.0.1 (compiled: yes)
    $ mypy --tb <file.py>
    Traceback (most recent call last):
      File ".venv/bin/mypy", line 8, in <module>
        sys.exit(console_entry())
      File ".venv/lib/python3.10/site-packages/mypy/__main__.py", line 15, in console_entry
        main()
      File "mypy/main.py", line 95, in main
      File "mypy/main.py", line 174, in run_build
      File "mypy/build.py", line 194, in build
      File "mypy/build.py", line 277, in _build
      File "mypy/build.py", line 2923, in dispatch
      File "mypy/build.py", line 3320, in process_graph
      File "mypy/build.py", line 3443, in process_stale_scc
      File "mypy/build.py", line 2504, in write_cache
      File "mypy/build.py", line 1574, in write_cache
      File "mypy/nodes.py", line 377, in serialize
      File "mypy/nodes.py", line 3851, in serialize
      File "mypy/nodes.py", line 3788, in serialize
      File "mypy/nodes.py", line 3524, in serialize
      File "mypy/types.py", line 2699, in serialize
      File "mypy/types.py", line 610, in serialize
      File "mypy/types.py", line 2880, in serialize
    AssertionError: Internal error: unresolved placeholder type None
    

    Unfortunately, the code base is large and proprietary, so I can't supply the code. I tried to figure out which exactly causes it, but was unable to narrow it down to anything concrete. Is there anything I can set to have mypy output more details? I tried -v, but the crash is in the phase where it doesn't really output anything.

  10. ronf commented on Mar 2, 2023

    @ronf

    I don't know if it will help or not, but here's how I worked around this problem in AsyncSSH after upgrading to mypy 1.0.0:

    ronf/asyncssh@0d49161

    Basically, I eliminated a type definition of the form Callable[[[], T], where T was a TypeVar that could be one of two types. By using the Callable type directly in places where it was needed rather than assigning it and using that assignment, the problem went away.

  11. jindongyang94 commented on Oct 23, 2024

    @jindongyang94

    Hi @Tasssadar , are you able to resolve your issue yet? Just a shot in the dark as I am currently experiencing the same issue and error, perhaps you installed pandas-stubs and it is causing your codebase with pandas to face this issue.

    For me, that was the problem and I have raised an issue with pandas-stubs separately. Hope this helps!

  12. Tasssadar commented on Oct 23, 2024

    @Tasssadar

    I haven't seen this in a while, so probably fixed in one of the MyPy updates that arrived in the meantime. But it's definitely not a pandas-stubs issue, this is mypy hard-crashing, that is never the right behavior - at the very least, it must produce some helpful message about what is wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions