Skip to content

Instantly share code, notes, and snippets.

@nwjlyons
Last active August 19, 2023 10:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nwjlyons/df0ac0a45bd7619412b827e976d24e17 to your computer and use it in GitHub Desktop.
Save nwjlyons/df0ac0a45bd7619412b827e976d24e17 to your computer and use it in GitHub Desktop.
Python isinstance checks behave differently from type annotations
>>> 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