Herramientas de usuario

Herramientas del sitio


wiki2:python:typing

Diferencias

Muestra las diferencias entre dos versiones de la página.

Enlace a la vista de comparación

Próxima revisión
Revisión previa
wiki2:python:typing [2022/03/14 17:35]
alfred creado
wiki2:python:typing [2022/10/12 19:03] (actual)
Línea 1: Línea 1:
 ====== Python Typing ====== ====== Python Typing ======
 +
 +Devolver el tipo de la misma clase:
 +<​code>​
 +from typing import Self
 +
 +class Foo:
 +   def returns_self(self) -> Self:
 +      return self
 +</​code>​
 +
 +Alias de tipo:
 +<​code>​
 +from typing import TypeAlias
 +
 +Factors: TypeAlias = list[int]
 +</​code>​
 +
 +Definición de un tipo (en este caso self):
 +<​code>​
 +from typing import TypeVar
 +
 +Self = TypeVar("​Self",​ bound="​Foo"​)
 +
 +class Foo:
 +   def returns_self(self:​ Self) -> Self:
 +      return self
 +</​code>​
 +
 +Several types:
 +<​code>​
 +Union[Union[int,​ str], float] == Union[int, str, float]
 +Union[int] == int
 +</​code>​
 +
 +Type or None:
 +''​Optional[X]''​ is equivalent to ''​X | None''​ (or ''​Union[X,​ None]''​).
 +<​code>​
 +Optional[str]
 +</​code>​
 +
 +Class type:
 +<​code>​
 +class ShopifyPostView(MethodView):​
 +    shopify_request_schema:​ t.Union[
 +        t.Type[ShopifyPaymentsRequest],​
 +        t.Type[ShopifyRefundsRequest],​
 +    ] = None
 +</​code>​
 +
 +Object of the previous class type:
 +<​code>​
 +ShopifyRequestClass:​ t.Type = t.Optional[
 +    t.Union[
 +        t.Type[ShopifyPaymentsRequest],​
 +        t.Type[ShopifyRefundsRequest],​
 +    ]
 +]
 +
 +ShopifyRequest = t.Type[ShopifyRequestClass]
 +</​code>​
 +
 +====== Links ======
 +
 +  * https://​towardsdatascience.com/​12-beginner-concepts-about-type-hints-to-improve-your-python-code-90f1ba0ac49
  
wiki2/python/typing.1647279335.txt.gz · Última modificación: 2022/03/14 18:35 (editor externo)