Skip to content

[stubtest] Fix false positives for type[TypeVar] defaults#20950

Open
emmanuel-ferdman wants to merge 2 commits intopython:masterfrom
emmanuel-ferdman:stubtest-type-typevar
Open

[stubtest] Fix false positives for type[TypeVar] defaults#20950
emmanuel-ferdman wants to merge 2 commits intopython:masterfrom
emmanuel-ferdman:stubtest-type-typevar

Conversation

@emmanuel-ferdman
Copy link

PR Summary

While working on django-stubs I find out that stubtest was reporting false positives when checking default values for parameters typed as type[TypeVar]. Classes with custom metaclasses (like ABC, Enum, or Django models) would fail because stubtest fell back to comparing metaclass types instead of the class itself. This PR also fixes a related issue where type context wasn't propagated to tuple elements, causing tuple[type[list[_T]], ...] to fail with a (list,) default.

Example 1:

# stub
from abc import ABC
from typing import TypeVar
class Handler(ABC): ...
_H = TypeVar("_H", bound=Handler)
def get_handler(cls: type[_H] = ...) -> _H: ...

# runtime
from abc import ABC
class Handler(ABC): pass
def get_handler(cls=Handler): return cls()

Output before fix:

error: get_handler is inconsistent, runtime argument "cls" has a default value of type "ABCMeta", which is incompatible with stub argument type "type[Handler]"

Example 2:

# stub
from typing import TypeVar
_T = TypeVar("_T")
def process(types: tuple[type[list[_T]], ...] = ...) -> None: ...

# runtime
def process(types=(list,)): pass

Output before fix:

error: process is inconsistent, runtime argument "types" has a default value of type "tuple[type[list[Any]]]", which is incompatible with stub argument type "tuple[type[list[_T]], ...]"

Fixes #13316
Fixes #19852

Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
Signed-off-by: Emmanuel Ferdman <emmanuelferdman@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant