Giter Site home page Giter Site logo

Comments (1)

erictraut avatar erictraut commented on June 3, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.