Git Product home page Git Product logo

Comments (5)

stevenh avatar stevenh commented on May 31, 2024

Workaround is to change else: -> elif type(val) is Duration: but this should be unnecessary.

from mypy.

stevenh avatar stevenh commented on May 31, 2024

Using isinstance instead of a type check doesn't report an issue but in this case where int is a parent class of Duration that test is incorrect.

def output(val: Duration | int) -> None:
    if isinstance(val, int): # Invalid due to Duration being a sub class of int
        print(f"type: {type(val)} = int: {val}")
    else:
        print(f"type: {type(val)} = duration: {val.nanoseconds()}") # Never called

from mypy.

erictraut avatar erictraut commented on May 31, 2024

Mypy's behavior is correct here. The type cannot be safely narrowed to just Duration in the "else" case. It's possible for other subtypes of int to be passed to the function, and they would not pass the test type(val) is int.

class OtherIntSubclass(int):
    pass

output(OtherIntSubclass(1))

You've already identified the correct way to fix this bug in your code — by using isinstance.

from mypy.

stevenh avatar stevenh commented on May 31, 2024

Thanks for clarification, coming from other languages my expectation was for exact type matching requirement from hint and hence Duration | int meant only Duration or int exactly, so OtherIntSubclass was not valid.

I believe what you're saying is this is not the case and instead Duration or int means those classes or any subclasses, is that correct @erictraut?

from mypy.

stevenh avatar stevenh commented on May 31, 2024

Answering my own question from PEP 484

Expressions whose type is a subtype of a specific argument type are also accepted for that argument.

Capturing for others the fix for this case is to invert the test an look for the more specific type, which is easy in this case given one is base type, not so easy for other types as you need know the types of the entire chain.

def output(val: Duration | int) -> None:
    if isinstance(val, Duration):
        print(f"type: {type(val)} = duration: {val.nanoseconds()}")
    else:
        print(f"type: {type(val)} = int: {val}")

Thanks for the quick response, closing as it is working as intended.

from mypy.

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.