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
| from __future__ import annotations | |
| import re | |
| import uuid | |
| from dataclasses import dataclass, field, replace | |
| from datetime import date, datetime, UTC | |
| from typing import Optional, Self, Literal | |
| def _normalize(value: str) -> str: |
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
| from dataclasses import dataclass, field | |
| from typing import TYPE_CHECKING | |
| @dataclass | |
| class NormalDataClass(): | |
| a : str | |
| b : int | |
| c : int = field(init=False) | |
| NormalDataClass(a='', b=0, c=0) |
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
| from typing import reveal_type | |
| import random | |
| def choice[T](left: T, right: T) -> T: | |
| if random.random() < 0.5: | |
| return left | |
| else: | |
| return right | |
| reveal_type(choice(3, "hey!")) |
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
| from collections.abc import Callable | |
| from typing import reveal_type | |
| class Foo: | |
| def __init__(self, size: str) -> None: | |
| self.size = size | |
| def create_big_foo() -> Foo: | |
| return Foo('big') | |
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
| from typing import Any, Optional, cast, TypeVar, reveal_type | |
| ## Case 1: type variable in return position of function | |
| def f[T]() -> T: | |
| raise Exception() | |
| # mypy correctly refuses to type this, as it cannot infer T from the call site. | |
| reveal_type(f()) |
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
| from typing import Any, Optional, cast, TypeVar, reveal_type | |
| ## Case 1: type variable in return position of function | |
| def f[T]() -> T: | |
| raise Exception() | |
| # mypy correctly refuses to type this, as it cannot infer T from the call site. | |
| reveal_type(f()) |
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
| from __future__ import annotations | |
| from typing import overload, Iterator | |
| class Foo: | |
| @overload | |
| def method(self: Bar) -> Iterator[Bar]: ... | |
| @overload | |
| def method(self: Foo) -> Iterator[Foo]: ... | |
| def method(self) -> Iterator[Foo] | Iterator[Bar]: | |
| raise NotImplementedError |
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
| from typing import Protocol | |
| class P(Protocol): | |
| def not_abstractmethod(self): ... | |
| class Q(P): ... | |
| Q() |
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
| from typing import Callable | |
| class C[T]: ... | |
| def f[U]() -> type[C[U]]: | |
| return C | |
| c: Callable[[], C[int]] = f() | |
| reveal_type(f) | |
| reveal_type(c) |
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
| from typing import Callable | |
| class C[T]: ... | |
| def f[U]() -> type[C[U]]: | |
| return C | |
| c: Callable[[], C[int]] = f() | |
| reveal_type(f) | |
| reveal_type(c) |
NewerOlder