This file contains 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 Iterator | |
def fib(n: int) -> Iterator[int]: | |
a, b = 0, 1 | |
while a < n: | |
yield a | |
a, b = b, a + b |
This file contains 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 Literal, Optional, overload | |
@overload | |
def gimme(x: Literal[True]) -> str: ... | |
@overload | |
def gimme(x: Literal[False]) -> None: ... | |
@overload | |
def gimme(x: bool) -> Optional[str]: ... | |
def gimme(x: bool) -> Optional[str]: |
This file contains 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 | |
def foo(x: Any) -> None: | |
pass | |
class Bar(): | |
pass |
This file contains 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 typing as tp | |
if not tp.TYPE_CHECKING: | |
reveal_type = lambda var: var | |
class Foo: # Only supports "*Foo*". | |
def __init__(self, value: int) -> None: | |
self.value = value | |
def __add__(self, other: tp.Union["Foo", "FooBar"]) -> "Foo": |
This file contains 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 fractions import Fraction | |
import decimal as dec | |
import typing as tp | |
if not tp.TYPE_CHECKING: | |
reveal_type = lambda var: var | |
Real = tp.Union[int, float, Fraction] | |
Decimal = tp.Union[dec.Decimal, int] | |
T1 = tp.TypeVar("T1", Real, Decimal) |
This file contains 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 | |
from dataclasses import is_dataclass | |
def example1(arg: Any) -> None: | |
if is_dataclass(arg): | |
reveal_type(arg) # ❌ Revealed type is "type[_typeshed.DataclassInstance]" | |
def example2(arg: object) -> None: |
This file contains 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
class Base:... | |
class Foo(Base):... | |
a: Base | |
b: int|None = {Foo:1}.get(type(a)) |
This file contains 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
a: object = 1 | |
reveal_type(a) |
This file contains 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 TypeVar | |
T1 = TypeVar("T1") | |
T2 = TypeVar("T2") | |
def f(t1: T1, t2: T2) -> T1 | T2: | |
return t1 |
This file contains 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 TypeVar | |
T1 = TypeVar("T2") | |
T2 = TypeVar("T2") | |
def f(t1: T1, t2: T2) -> T1 | T2: ... | |
b: int = f(1, None) or 1 |
NewerOlder