Python’s pathlib get parent’s relative path

Use the PurePath.relative_to() method to produce a relative path. You weren’t very clear as to how the base path is determined; here are two options: secondparent = path.parent.parent homedir = pathlib.Path(r’C:\users\user1′) then just use str() on the path.relative_to(secondparent) or path.relative_to(homedir) result. Demo: >>> import pathlib >>> path = pathlib.Path(r’C:\users\user1\documents\importantdocuments’) >>> secondparent = path.parent.parent >>> homedir … Read more

type hint for an instance of a non specific dataclass

Despite its name, dataclasses.dataclass doesn’t expose a class interface. It just allows you to declare a custom class in a convenient way that makes it obvious that it is going to be used as a data container. So, in theory, there is little opportunity to write something that only works on dataclasses, because dataclasses really … Read more

How to make “keyword-only” fields with dataclasses?

Update: coming in Python 3.10, there’s a new dataclasses.KW_ONLY sentinel that works like this: @dataclasses.dataclass class Example: a: int b: int _: dataclasses.KW_ONLY c: int d: int Any fields after the KW_ONLY pseudo-field are keyword-only. There’s also a kw_only parameter to the dataclasses.dataclass decorator, which makes all fields keyword-only: @dataclasses.dataclass(kw_only=True) class Example: a: int b: … Read more

“ indirect fixture” error using pytest. What is wrong?

TL;DR – The problem is with the line @pytest.mark.parametrize(“entrada”,”esperado”,[ … ]) It should be written as a comma-separated string: @pytest.mark.parametrize(“entrada, esperado”,[ … ]) You got the indirect fixture because pytest couldn’t unpack the given argvalues since it got a wrong argnames parameter. You need to make sure all parameters are written as one string. Please … Read more

How do I document a constructor for a class using Python dataclasses?

The napoleon-style docstrings as they are described in the sphinx docs (see the ExampleError class for their take on it) explicitly touch on your case: The __init__ method may be documented in either the class level docstring, or as a docstring on the __init__ method itself. And if you do not want this behavior, you … Read more

Python 3.7 on Ubuntu 20.04

Do you need Ubuntu 20.04? Ubuntu 18.04 comes with Python 3.6, and 3.7 available. If you do, the deadsnakes PPA has Python 3.5-3.7 for Ubuntu 20.04 (Focal). To add it and install: sudo add-apt-repository ppa:deadsnakes/ppa sudo apt-get install python3.7 P.s. I’m not a dev and have no experience with Tensorflow so take this with a … Read more

AttributeError: type object ‘Callable’ has no attribute ‘_abc_registry’

Try uninstalling typing: pip uninstall typing or downgrading python to 3.6. There seems to be a problem with typing in 3.7 If after that you get: AttributeError: module ‘typing’ has no attribute ‘_ClassVar’ try pip uninstall dataclasses. For more info refer to: https://github.com/RTIInternational/gobbli/issues/10

How can I get Python 3.7 new dataclass field types?

Inspecting __annotations__ gives you the raw annotations, but those don’t necessarily correspond to a dataclass’s field types. Things like ClassVar and InitVar show up in __annotations__, even though they’re not fields, and inherited fields don’t show up. Instead, call dataclasses.fields on the dataclass, and inspect the field objects: field_types = {field.name: field.type for field in … Read more