Last active
August 19, 2023 10:04
-
-
Save nwjlyons/df0ac0a45bd7619412b827e976d24e17 to your computer and use it in GitHub Desktop.
Python isinstance checks behave differently from type annotations
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
>>> isinstance(1, int) | |
True # π | |
>>> isinstance(1, float) | |
False # π | |
>>> isinstance(1.0, int) | |
False # π | |
>>> isinstance(1.0, float) | |
True # π | |
def integer_integer() -> int: | |
return 1 # π | |
def integer_float() -> float: | |
return 1 # β no complaint from mypy for returning an integer with float annotation | |
def float_integer() -> int: | |
return 1.0 # π mypy complains | |
def float_float() -> float: | |
return 1.0 # π |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment