python / mypy

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

Subtype for `memoryview` required to pass mypy checks but triggers`TypeError` in Python #18053

Closed lgautier closed 2 hours ago

lgautier commented 2 hours ago

Bug Report

Specifying a subtype for memoryview is required to pass mypy checks, but this create a TypeError in Python.

To Reproduce

This fails mypy checks:

import typing

def foo(m: memoryview) -> memoryview:
    return m

def bar(n: int, t: typing.Literal['i', 'd']) -> memoryview:
    return memoryview(bytes(n)).cast(t)

res = foo(bar(8, 'i'))
$ mypy test-type-hints.py 
test-type-hints.py:7: error: Incompatible return value type (got "memoryview[int] | memoryview[float]", expected "memoryview[int]")  [return-value]
Found 1 error in 1 file (checked 1 source file)
$ python test-type-hints.py 

Fixing mypy checks triggers Python error:

import typing

def foo(m: memoryview[int]) -> memoryview[int]:
    return m

@typing.overload
def bar(n: int, t: typing.Literal['i']) -> memoryview[int]: ...

@typing.overload
def bar(n: int, t: typing.Literal['d']) -> memoryview[float]: ...

def bar(n: int, t: typing.Literal['i', 'd']) -> memoryview[int] | memoryview[float]:
    return memoryview(bytes(n)).cast(t)

res = foo(bar(8, 'i'))
$ mypy test-type-hints.py 
Success: no issues found in 1 source file
$ python test-type-hints.py 
Traceback (most recent call last):
  File "/some/path/test-type-hints.py", line 3, in <module>
    def foo(m: memoryview[int]) -> memoryview[int]:
               ~~~~~~~~~~^^^^^
TypeError: type 'memoryview' is not subscriptable

Expected Behavior

$ mypy test-type-hints.py
Success: no issues found in 1 source file
$ python test-type-hints.py 

Actual Behavior

Either mypy fails but Python can run the code, or mypy passes but Python doesn't run.

Your Environment

$ mypy --version
mypy 1.13.0 (compiled: yes)
$ python --version
Python 3.12.3
brianschubert commented 2 hours ago

Hi! This is https://mypy.readthedocs.io/en/stable/runtime_troubles.html#using-classes-that-are-generic-in-stubs-but-not-at-runtime

Try using from __future__ import annotations, string literal annotations (memoryview[int] -> "memoryview[int]"), or an if TYPE_CHECKING: alias.

brianschubert commented 2 hours ago

(memoryview was made generic in typeshed quite recently in https://github.com/python/typeshed/pull/12247. I don't the background here, but it seems like if memoryview is going to be generic in the stubs, we should eventually add memoryview.__class_getitem__ upstream? I couldn't find an existing CPython issue for this)

lgautier commented 2 hours ago

Thanks. Adding from __future__ import annotations does solve the issue. I had understood from the doc page you mentioned that this was only for Python 3.8 or earlier.