diff --git a/python-ty/README.md b/python-ty/README.md new file mode 100644 index 0000000000..1d35ad7c68 --- /dev/null +++ b/python-ty/README.md @@ -0,0 +1,3 @@ +# Astral's ty: A New Blazing-Fast Type Checker for Python + +This folder contains sample code for the Real Python tutorial [Astral's ty: A New Blazing-Fast Type Checker for Python](https://realpython.com/python-ty/). diff --git a/python-ty/adder.py b/python-ty/adder.py new file mode 100644 index 0000000000..dc53037a5a --- /dev/null +++ b/python-ty/adder.py @@ -0,0 +1,28 @@ +# fmt:off + +import warnings + +type Number = int | float + + +def new_adder(x: Number, y: Number) -> Number: + return x + y + + +@warnings.deprecated("Use the new_adder() instead") +def legacy_adder(x, y): + return x + y + + +if __name__ == "__main__": + legacy_adder( # ty: ignore + 42, + 555 + ) + + legacy_adder( + 42, + 555 + ) # ty: ignore + + new_adder(a=42, b=555) # ty: ignore[unknown-argument, missing-argument] diff --git a/python-ty/cross_platform.py b/python-ty/cross_platform.py new file mode 100644 index 0000000000..41a75e1586 --- /dev/null +++ b/python-ty/cross_platform.py @@ -0,0 +1,9 @@ +from sys import platform +from typing import reveal_type + +if platform == "win32": + x = "Windows" +else: + x = "macOS or Linux" + +reveal_type(x) diff --git a/python-ty/python_version.py b/python-ty/python_version.py new file mode 100644 index 0000000000..56616e2cd9 --- /dev/null +++ b/python-ty/python_version.py @@ -0,0 +1,3 @@ +from typing import Literal + +type Size = Literal["S", "M", "L", "XL"]