17th February 2022
Coding⚑
Python⚑
asyncio⚑
-
New: Add tutorial on how to use asyncio.
-
New: Version overriding now supports constrains.
Before you had to pin specific versions, which is not maintainable, now you can use constrains
[tool.pdm.overrides] asgiref = ">=3.2.10"
-
pdm update --dry-run --unconstrained
Pydantic Factories⚑
-
Correction: Correct the type hints of the factory.
Use
Any
class PersonFactory(ModelFactory[Any]): ...
FastAPI⚑
-
Probably you've introduced an ending
/
to the endpoint, so instead of asking for/my/endpoint
you tried to do/my/endpoint/
.
Feedparser⚑
Pytest⚑
-
Correction: Update the tmpdir_factory type hints.
You should now use
TempPathFactory
instead ofTempdirFactory
-
Correction: Use
pytest-freezegun
globally.Most of the tests work with frozen time, so it's better to freeze it by default and unfreeze it on the ones that actually need time to move.
To do that set in your
tests/conftest.py
a globally used fixture:if TYPE_CHECKING: from freezegun.api import FrozenDateTimeFactory @pytest.fixture(autouse=True) def frozen_time() -> Generator['FrozenDateTimeFactory', None, None]: """Freeze all tests time""" with freezegun.freeze_time() as freeze: yield freeze
-
New: Ignore a warning of a specific package.
In the
pyproject.toml
filterwarnings = [ "error", # Until https://github.com/ktosiek/pytest-freezegun/issues/35 is merged "ignore::DeprecationWarning:pytest_freezegun.*" ]
Python Snippets⚑
-
Warning messages are typically issued in situations where it is useful to alert the user of some condition in a program, where that condition (normally) doesn’t warrant raising an exception and terminating the program. For example, one might want to issue a warning when a program uses an obsolete module.
import warnings def f(): warnings.warn('Message', DeprecationWarning)
To test the function with pytest you can use
pytest.warns
:import warnings import pytest def test_warning(): with pytest.warns(UserWarning, match='my warning'): warnings.warn("my warning", UserWarning)
-
New: Parse XML file with beautifulsoup.
You need both
beautifulsoup4
andlxml
:bs = BeautifulSoup(requests.get(url), "lxml")
-
New: Get a traceback from an exception.
import traceback traceback_str = ''.join(traceback.format_tb(e.__traceback__))