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

"unresolved placeholder type None" on mypy 1.0.0 #14631

Closed
jonathanslenders opened this issue Feb 7, 2023 · 6 comments · Fixed by #14642
Closed

"unresolved placeholder type None" on mypy 1.0.0 #14631

jonathanslenders opened this issue Feb 7, 2023 · 6 comments · Fixed by #14642
Labels

Comments

@jonathanslenders
Copy link

jonathanslenders commented Feb 7, 2023

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

@jonathanslenders jonathanslenders added the bug mypy got something wrong label Feb 7, 2023
@wjurkowlaniec
Copy link

I have the same problem..

@hauntsaninja
Copy link
Collaborator

hauntsaninja commented Feb 7, 2023

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

And thank you for the minimal repro here!

@jonathanslenders
Copy link
Author

@hauntsaninja : Thanks for the quick fix!!

@ronf
Copy link

ronf commented Feb 12, 2023

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

@Tasssadar
Copy link

Tasssadar commented Mar 2, 2023

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.

@ronf
Copy link

ronf commented Mar 2, 2023

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.

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.

6 participants