Comments (1)
This is by design, not a bug. This behavior was previously somewhat ambiguous in the typing spec, but the recent addition of PEP 696 (which introduces TypeVar default values) clarified how this must work.
When you call a constructor of a generic class, the specialized type of the class is not yet established and can be inferred from the arguments passed to the constructor.
By contrast, when you call a class method, the specialized type of the class is already established at the time that the method is bound to the class โย prior to the call. If you don't explicitly provide type argument values, they take on their default values (as explained in PEP 696). If no default is specified, it implicitly becomes Any
(i.e. Unknown
).
In your example above, if you want to invoke a class method Some
on Option[int]
, you'd need to write Option[int].Some(2)
. Or if you use PEP 696 to specify a default type argument for T
, you could do the following:
Code sample in pyright playground
from typing import Generic, reveal_type
from typing_extensions import TypeVar
T = TypeVar("T", default=int)
class Option(Generic[T]):
def __init__(self, value: T):
self._val = value
@classmethod
def Some(cls, val: T) -> "Option[T]":
return cls(val)
reveal_type(Option.Some(2)) # Option[int]
from pyright.
Related Issues (20)
- Expected type argument for generic class Series HOT 5
- Unexpected "Import cannot be resolved" HOT 4
- return type of subclass constructor incorrectly inferred as super class HOT 1
- Attempting to type narrow on builtin classes with phantom subclasses adds those classes into the narrowed result HOT 1
- `ClassVar[Final[T]]` should be allowed for dataclasses HOT 1
- False positive reportOptionalMemberAccess when non-trivial predicate is separate from `if` HOT 2
- Pyright ignores configuration from custom toml files HOT 4
- Typeguard is not None with list[int|None] and index subscript not working HOT 3
- Issues accessing `enum.nonmember` from another `nonmember` HOT 3
- python HOT 1
- obj.get("CONST") does not imply "CONST" in obj for reportTypedDictNotRequiredAccess HOT 1
- Version-aware `reportShadowedImports` HOT 3
- StrEnum `value` property type regression HOT 2
- `def __init__(self: Self)` causes `reportInvalidTypeVarUse` HOT 1
- reportOverlappingOverload false positive: variadic tuple unpacking HOT 2
- Generic types are reused / only bound once HOT 3
- Introduce `reportUnnecessaryTypeIs` rule HOT 3
- Support different type for class variable and instance variable HOT 3
- Different inferred type `t = tuple(); list(t)` and `list(tuple())` HOT 1
- Type hinting for tuples HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from pyright.