microsoft / microsoft/pyright

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

Open
#10,826 0 comments 3 reactions 0 assignees View on GitHub
bug
Dominant language
Python
Stars
15.6k
Forks
1.8k
Avg merge
12h 13m
Merged PRs (30d)
52

Description

**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:

```py
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+](https://github.com/python/typing/issues/2074). 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`:

```py
from typing import NewType

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

Despite typeshed [including this attribute](https://github.com/python/typeshed/blob/b9d15678cd8ff918311901d4a6229a31f0d2aa59/stdlib/typing.pyi#L393) 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

Contributor guide

Open the contributing guide

Research direction

Reproduce the two examples in the Pyright playground, focusing on how NewType objects are represented in diagnostics and attribute access. Compare the reported FunctionType behavior with the runtime NewType behavior and the typeshed __supertype__ definition; done means both examples report the appropriate NewType type and allow the documented attribute access.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
devtools
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.