#11736·pyright

`TypeVar` with `Never` as an option and unexpected `reportArgumentType`

Author: finite-state-machineCreated Sep 11, 2026Updated Sep 11, 2026
Labelsbug

Describe the bug

Given _Z = TypeVar('_Z', SomeType, Never), and class SomeClass(Generic[_Z]), you'd expect a function parameter of type SomeClass[_Z] would accept SomeClass[Never]. It does not.

Code or Screenshots Code sample in pyright playground

python
from typing import Generic, Never, TypeVar

_Z = TypeVar('_Z', int, Never)

class SomeClass(Generic[_Z]): ...
def some_func(_: SomeClass[_Z]) -> None: ...

instance_int = SomeClass[int]()
instance_never = SomeClass[Never]()

some_func(instance_int)  # ok (no output, as expected)
some_func(instance_never)
        # unexpected error! pyright says:
        # error: Argument of type "SomeClass[Never]" cannot be assigned
        #     to parameter "_" of type "SomeClass[_Z@some_func]" in
        #     function "some_func"
        # "SomeClass[Never]" is not assignable to "SomeClass[_Z@some_func]"
        # Type parameter "_Z@SomeClass" is invariant, but "Never" is
        #     not the same as "_Z@some_func" (reportArgumentType)

If this is intended behavior... What's the best workaround for a class which may or may not have (for example) a special-case placeholder value?

python
class SomeClass(Generic[_Z]):
    def foo_to_bar(self, value: float) -> int | _Z: ...
    def bar_to_foo(self, value: int | _Z) -> float: ...

Concrete example: a monotonic mapping from non-negative floats to another linear float space might be the identity function (lambda x: x) or might be log()/exp() with a special case for zero.

VS Code extension or command-line Tested with latest (1.1.414) on pyright-play.net, and via LSP.