pyright icon indicating copy to clipboard operation
pyright copied to clipboard

Pyright incorrectly refers to `NewType` as an instance of `FunctionType` in diagnostic messages, does not allow attributes to be accessed

Open AlexWaygood opened this issue 4 months ago • 0 comments

Describe the bug

Pyright sometimes (but not consistently, it appears) refers to objects returned by calls to NewType as being instances of types.FunctionType. E.g. for this code:

from typing import NewType

X = NewType("X", int)
isinstance(42, X)

Pyright says:

Argument of type "type[X]" cannot be assigned to parameter "class_or_tuple" of type "_ClassInfo" in function "isinstance"
  Type "FunctionType" is not assignable to type "_ClassInfo"
    "FunctionType" is not assignable to "type"
    "FunctionType" is not assignable to "UnionType"
    "FunctionType" is not assignable to "tuple[_ClassInfo, ...]"

Pyright is correct to emit an error on this code, but the explanation is quite confusing: X here isn't an instance of types.FunctionType at runtime on Python 3.10+. It's an instance of typing.NewType.

Similarly, pyright emits an error if you try to access the __supertype__ attribute exposed for introspection purposes at runtime on instances of typing.NewType:

from typing import NewType

X = NewType("X", int)
reveal_type(X.__supertype__)

Despite typeshed including this attribute in its stubs, pyright says:

Cannot access attribute "__supertype__" for class "FunctionType"
  Attribute "__supertype__" is unknown  (reportFunctionMemberAccess)
Type of "X.__supertype__" is "Any"

VS Code extension or command-line Pyright playground

AlexWaygood avatar Aug 20 '25 14:08 AlexWaygood