127 Posts Tagged ‘python’

(All tags.)

Django: Parameterized tests for all model admin classes

I’ve got a lovely bunch of parameterized tests, there they are a-standing in a row…

Here’s an application of “test smarter, not harder”, as per Luke Plant’s post. I came up with this recently whilst working on my client Silvr’s project, and I’m pretty proud of it. It should apply to any project using Django’s admin.


How to download a documentation website with Wget

Time to grab a whole document tree.

Sometimes you want to download a whole website so you have a local copy that you can browse offline. When programming, this is often useful for documentation that sites that do not provide downloadable versions, and are not available in offline tools like DevDocs.


Django: How to profile and improve startup time

A waterfall, as used several times in this post.

Your Django project’s startup time impacts how smooth it is to work with. Django has to restart your project every time you run a management command and when runserver reloads. This involves importing all your apps, and thus all the modules that they import.


unittest’s new context methods in Python 3.11 (with backports)

A testing concerto

Python 3.11 only made one change to unittest, but it’s a good one: context manager methods. These methods can simplify setup and teardown logic in many cases, such as dynamic use of unittest.mock.


Why does Python log a DeprecationWarning saying “invalid escape sequence”?

Invalid essssscape sssssequence...

Take this code, which prints a shruggy man:


Python Type Hints - How to use Mypy’s always-true boolean check detection

Redundant-looking science apparatus

Sometimes code uses boolean checks on variables that can only be true. This is normally a sign of a mistake, either in the type hints or the implementation. Mypy has an optional check that can find such problematic boolean usage with its truthy-bool error code.


Python Type Hints - Make Mypy disallow implicit optional types

Optional? ¯_(ツ)_/¯

The original type hint proposal, PEP 484, initially allowed implicit optional types in function signatures. That is, a parameter with a default value of None would have its type automatically interpreted as optional. For example, this signature:


Python Type Hints - Old and new ways to write the same types

This is how evolution works, right?

As type hints have evolved, Python has added simpler, more succinct syntaxes. But you still need to know the older forms, since Mypy uses them when reporting types.


Python Type Hints - How to Do Exhaustiveness Checking

Well, that looks exhuasting.

Exhaustiveness checking is a very handy type checker feature. It ensures that all possible types of a variable are handled. If your code changes to add another possible type, you can guarantee that exhaustiveness-checked code paths handle the new case.


Python Type Hints - Lambdas don’t support type hints, but that’s okay

A tangle of lambdas (generated by Stable Diffusion)

Python has no syntax to add type hints to lambdas, but that doesn’t mean you can’t use them in type-checked code. In this post we’ll look at how Mypy can infer the types for lambdas, based on where they’re used.