Last active
August 5, 2024 13:57
-
-
Save CodeByAidan/1770443d22e72ed34d2c80f5305beb95 to your computer and use it in GitHub Desktop.
will be updating this a lot whenever I want to use typing but with typing that's not present in typing library. mypy + pylance ready! Python 3.12+
This file contains hidden or 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
| import sys | |
| from typing import TypeVar | |
| __all__: list[str] = [ | |
| "Any", | |
| "cast", | |
| "reveal_type", | |
| ] | |
| class _AnyMeta(type): | |
| """ | |
| A metaclass for `better_typing.Any` that provides custom behavior for | |
| ``__instancecheck__`` and ``__repr__`` methods. | |
| Changes: | |
| -------- | |
| - Added type annotations to the ``__instancecheck__`` method. | |
| - Added type return hint `str` to the ``__repr__`` method. | |
| - Simplified the ``__repr__`` method by using a ternary operator. | |
| """ | |
| def __instancecheck__(self, obj: object) -> bool: | |
| if self is Any: | |
| raise TypeError("better_typing.Any cannot be used with isinstance()") | |
| return super().__instancecheck__(obj) | |
| def __repr__(self) -> str: | |
| return "better_typing.Any" if self is Any else super().__repr__() | |
| class Any(metaclass=_AnyMeta): | |
| """Special type indicating an unconstrained type. | |
| - `Any` is compatible with every type. | |
| - `Any` is assumed to have all methods. | |
| - All values are assumed to be instances of `Any`. | |
| Note that all the above statements are true from the point of view of | |
| static type checkers. At runtime, `Any` should not be used with instance | |
| checks. | |
| Changes: | |
| -------- | |
| - Removed the `*args` and `**kwargs` from the ``__new__`` method. | |
| - Added `"Any"` to the ``__new__`` method's return type hint. | |
| """ | |
| def __new__(cls) -> "Any": | |
| if cls is Any: | |
| raise TypeError("Any cannot be instantiated") | |
| return super().__new__(cls) | |
| T = TypeVar("T") | |
| def cast(typ: type[T], val: object, /) -> T: | |
| """Cast a value to a type. | |
| This returns the value unchanged. To the type checker this | |
| signals that the return value has the designated type, but at | |
| runtime we intentionally don't check anything (we want this | |
| to be as fast as possible). | |
| Changes: | |
| -------- | |
| - Added type annotations to the parameters and return type | |
| using the generic type `T`. | |
| - Added a positional-only parameter indicator (`/`). | |
| - Added a `# type: ignore` comment to the return statement. | |
| """ | |
| return val # type: ignore | |
| def reveal_type[T](obj: T, /) -> T: | |
| """Ask a static type checker to reveal the inferred type of an expression. | |
| When a static type checker encounters a call to ``reveal_type()``, | |
| it will emit the inferred type of the argument:: | |
| x: int = 1 | |
| reveal_type(x) | |
| Running a static type checker (e.g., mypy) on this example | |
| will produce output similar to 'Revealed type is "builtins.int"'. | |
| At runtime, the function prints the runtime type of the | |
| argument and returns the argument unchanged. | |
| """ | |
| print(f"Runtime type is {type(obj).__name__!r}", file=sys.stderr) | |
| return obj |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment