def donde(self, cond: str) -> Self: self._filters.append(cond) return self def build(self) -> str: return ” AND “.join(self._filters) clase AdvancedQuery(Query): def order_by(self, col: str) -> Self: return self q = AdvancedQuery().where(“edad > 18”).order_by(“nombre”) revela_tipo(q) Vector: TypeAlias = lista[float]
Matriz: TypeAlias = lista[Vector]
def punto(a: Vector, b: Vector) -> float: devolver suma(x * y para x, y en zip(a, b)) v1: Vector = [1.0, 2.0, 3.0]
v2: Vector = [4.0, 5.0, 6.0]
punto(v1, v2) punto(v1, [1, 2, 3]) UserId = NewType(“UserId”, int) OrderId = NewType(“OrderId”, int) def get_user(uid: UserId) -> str: return f”user_{uid}” uid = UserId(42) oid = OrderId(99) get_user(uid) get_user(oid) get_user(42) “””) print(“→ s10_modern_types.py:”) run_pyright(“s10_modern_types.py”) print(“=” * 62) print(“SECCIÓN 11 · revelar_type() & type: ignore”) print(“=” * 62) write(“s11_reveal_ignore.py”, “”” al escribir import Cualquier valor = [1, “two”, 3.0]
revelar_tipo(valores) def misterio(x: Cualquiera) -> Cualquiera: devolver x r = misterio(42) revelar_tipo(r) malo: int = “ups” malo2: int = “también malo” # tipo: ignorar[assignment]
“””) print(“→ s11_reveal_ignore.py:”) run_pyright(“s11_reveal_ignore.py”) print(“=” * 62) print(“TUTORIAL COMPLETO”) print(“=” * 62) print(“”” Temas cubiertos ────────────── 1 Anotaciones e inferencia básicas 2 Sintaxis opcional/Unión/PEP 604 3 Limitación de tipos (isinstance, guards, TypeGuard, match) 4 Genéricos: TypeVar, Generic, ParamSpec 5 Protocolos y subtipos estructurales 6 TypedDict, clases de datos, NamedTuple 7 Literal, Final, @overload 8 Modo estricto 9 pyrightconfig.json 10 Self, TypeAlias, NewType 11 reveló_tipo() y tipo: ignorar Todos los archivos fuente escritos en: /tmp/pyright_tutorial/ “””)