157 Posts Tagged ‘python’ (Page 2)

(All tags.)


My appearance on The Real Python Podcast 179

I had the pleasure of returning to The Real Python podcast in last week’s episode 179, Improving Your Git Developer Experience in Python. It was great to catch up with host Christopher Bailey and chat about:

Read more...

GitHub Actions: Faster Python runs with cached virtual environments

Most projects I work on use Python, good ol’ Pip, and pip-tools. Below is a pattern I’ve used to speed up the GitHub Actions workflow runs on several such projects. On larger projects with many dependencies, it can save tens of seconds per run.

Read more...

Introducing flake8-logging

The Python standard library’s logging module is a go-to for adding observability to applications. Many tools also integrated with it or enhance its capabilities, such as structlog and Sentry.

Read more...

Python: Profile a section of code with cProfile

When trying to improve a slow function or module, it’s always a good idea to profile it. Here’s a snippet for quickly profiling a section of code with Python’s cProfile module, in two flavours. It’s adapted from the cProfile documentation’s Profile example. I have used versions of this snippet over the years to narrow in on performance issues.

Read more...

Python type hints: How to pass Any for unused parameters in tests

When you create a function to match an interface, it often needs to accept parameters that it doesn’t use. Once you introduce type hints, testing such functions can become a little irksome as Mypy will require all arguments to have the correct types. Your tests can end up creating unused objects only to match the tested function’s signature. Here’s a technique to avoid that work.

Read more...

Python type hints: modernized error messages in Mypy 1.4.0

Mypy 1.4.0 was released last week (2023-06-20). I’m happy to see it includes three improvements that modernize error messages with newer type hint syntax:

Read more...

Django: Parametrized tests for all model admin classes

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.

Read more...

How to download a documentation website with Wget

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.

Read more...

Django: How to profile and improve startup time

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.

Read more...

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

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.

Read more...

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

Take this code, which prints a shruggy man:

Read more...

Python type hints: use Mypy’s always-true boolean check detection

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.

Read more...

Python type hints: make Mypy disallow implicit optional types

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:

Read more...

Python type hints: old and new ways to write the same types

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.

Read more...

Python type hints: exhaustiveness checking

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.

Read more...

Python type hints: lambdas don’t support type hints, but that’s okay

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.

Read more...

Python Type Hints: How to Gradually Add Types for Third Party Packages

Hynek Schlawack recently described graduality as Python’s super power: the ability to prototype in the REPL, and gradually add linting, type checking, and other practices to refine your code into maintainable, production-ready software. You can also apply graduality within tools, activating checks one at a time and fixing the resulting errors as you go.

Read more...

Django: use partial() with transaction.on_commit() to avoid late-binding bugs

Django’s transaction.on_commit() allows you to run a function after the current database transaction is committed. This is useful to ensure that actions with external services, like sending emails, don’t run until the relevant data is definitely saved.

Read more...

How to Patch Requests to Have a Default Timeout

Python’s requests package is very popular. Even if you don’t use it directly, it’s highly likely one of your dependencies does.

Read more...

Appearance on Podcast.__init__

I joined host Tobias Macey on Podcast.__init__, in Episode 349, published this monday. The episode is titled “Improve Your Productivity By Investing In Developer Experience Design For Your Projects”. We covered various topics related to “developer experience” and general ways to improve it on any Python project.

Read more...

A Problem with Duplicated Mutable Constants

Here’s a small problem I’ve seen where several modules share versions of the same “constant” variable. It came up in the context of a Django project with multiple settings files, but it could happen in different contexts.

Read more...

Python type hints: split types by Python version

The typing module continues to evolve, with new features in every Python version. This can make it tricky if you’re trying to type code that supports multiple Python versions. To help write such code, Mypy identifies version checks using sys.version_info and reads the appropriate branch.

Read more...

How to Fix a Python “SyntaxError: invalid character” Caused by Curly Quotes

Here’s an innocent enough looking Python file:

Read more...

Python: make simple mocks with SimpleNamespace

When testing Python code you may need a mock object. That’s okay! But what’s the best way to build a simple mock?

Read more...

Book-Driven Development from “Boost Your Django DX”

On Monday I released my new book “Boost Your Django DX”. It covers many tools and practices that are useful for developing Django projects.

Read more...