18th November 2021
Coding⚑
Python⚑
Type Hints⚑
-
New: Define a TypeVar with restrictions.
from typing import TypeVar AnyStr = TypeVar('AnyStr', str, bytes)
-
New: Use a constrained TypeVar in the definition of a class attributes.
If you try to use a
TypeVar
in the definition of a class attribute:class File: """Model a computer file.""" path: str content: Optional[AnyStr] = None # mypy error!
mypy will complain with
Type variable AnyStr is unbound [valid-type]
, to solve it, you need to make the class inherit from theGeneric[AnyStr]
.class File(Generic[AnyStr]): """Model a computer file.""" path: str content: Optional[AnyStr] = None
Properties⚑
- New: Give an overview on Python's @property decorator.
Pydantic⚑
-
New: Define fields to exclude from exporting at config level.
Eagerly waiting for the release of the version 1.9 because you can define the fields to exclude in the
Config
of the model using something like:class User(BaseModel): id: int username: str password: str class Transaction(BaseModel): id: str user: User value: int class Config: fields = { 'value': { 'alias': 'Amount', 'exclude': ..., }, 'user': { 'exclude': {'username', 'password'} }, 'id': { 'dump_alias': 'external_id' } }
The release it's taking its time because the developer's gremlin and salaried work are sucking his time off.