Skip to content

Instantly share code, notes, and snippets.

@jaycosaur
Last active August 9, 2020 23:20
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 jaycosaur/fc77323051901cb7820c6a5dba023cd1 to your computer and use it in GitHub Desktop.
Save jaycosaur/fc77323051901cb7820c6a5dba023cd1 to your computer and use it in GitHub Desktop.
Strong or Weak [python] - Typescript to Python field guide
from typing import NewType
Celsius = NewType("Celsius", float)
Fahrenheit = NewType("Fahrenheit", float)
def convert_to_fahrenheit(value: Celsius) -> Farenheit:
return Farenheit((value * 9 / 5) + 32)
def convert_to_celsius(value: Fahrenheit) -> Celsius:
return Celsius((value - 32) * 5 / 9)
converted = convert_to_celsius(
0
) # Argument 1 to "convert_to_celsius" has incompatible type "int"; expected "Fahrenheit"
converted = convert_to_celsius(Fahrenheit(0)) # it works!
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment