How to add a dataclass field without annotating the type?

The dataclass decorator examines the class to find fields, by looking for names in __annotations__. It is the presence of annotation which makes the field, so, you do need an annotation. You can, however, use a generic one: @dataclass class Favs: fav_number: int = 80085 fav_duck: ‘typing.Any’ = object() fav_word: str=”potato”

Optional Union in type hint

You may think of the typing library as a specification on how to declare certain types. If something is not defined in that specification then it’s always better assume it to be an undefined behavior. However in the particular case of Python and typing we have a kind-of-reference static type checker which is mypy. So … Read more

Replace attributes in Data Class objects

The dataclasses module has a helper function for field replacement on instances (docs) from dataclasses import replace Usage differs from collections.namedtuple, where the functionality was provided by a method on the generated type (Side note: namedtuple._replace is documented/public API, using an underscore on the name was called a “regret” by the author, see link at … Read more

Python 3.7: check if type annotation is “subclass” of generic

First of all: There is no API defined to introspect type hinting objects as defined by the typing module. Type hinting tools are expected to deal with source code, so text, not with Python objects at runtime; mypy doesn’t introspect List[str] objects, it instead deals a parsed Abstract Syntax Tree of your source code. So, … Read more

How to await a coroutine in pdb

I had a similar problem debugging the useage of aiofile. I then found a solution using nest_asyncio. For example if one has the following async example script: import asyncio from aiofile import async_open import nest_asyncio async def main(): async with async_open(“/tmp/hello.txt”, ‘w+’) as afp: await afp.write(“Hello “) await afp.write(“world”) afp.seek(0) breakpoint() print(await afp.read()) if __name__==”__main__”: … Read more

ImportError: cannot import name ‘Literal’ from ‘typing’

Using Literal in Python 3.8 and later from typing import Literal Using Literal in all Python versions (1) Literal was added to typing.py in 3.8, but you can use Literal in older versions anyway. First install typing_extensions (pip install typing_extensions) and then from typing_extensions import Literal This approach is supposed to work also in Python … Read more

How can I set an attribute in a frozen dataclass custom __init__ method?

The problem is that the default __init__ implementation uses object.__setattr__() with frozen classes and by providing your own implementation, you have to use it too which would make your code pretty hacky: @dataclass(frozen=True, init=False) class Tricky: thing1: int thing2: str def __init__(self, thing3): object.__setattr__(self, “thing3”, thing3) Unfortunately, python does not provide a way to use … Read more

How to install python3.7 and create a virtualenv with pip on Ubuntu 18.04?

I don’t know if it’s best practices or not, but if I also install python3-venv and python3.7-venv then everything works (this is tested on a fresh stock Debian buster docker image): % sudo apt install python3.7 python3-venv python3.7-venv % python3.7 -m venv py37-venv % . py37-venv/bin/activate (py37-venv) % Note that it also installs all of … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)