172 Posts Tagged ‘python’ (Page 3)

(All tags.)


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...

Removing Python 3.6 Support from My Packages

Python 3.6 reached its end of life on the 23rd December. As its release manager put on the Python forum, it has gracefully “ridden off into the sunset”.

Read more...

How to Make an Immutable Dict in Python

Python’s built-in collection types come in mutable and immutable flavours, but one is conspicuously missing:

Read more...

Python type hints: handle optional imports

This post is not about importing typing.Optional, but instead imports that are themselves optional. Libraries often have optional dependencies, and the code should work whether or not the import is there. A common pattern to solve this to catch ImportError and replace the module with None:

Read more...

Set up a Gunicorn Configuration File, and Test It

If you use Gunicorn, it’s likely you have a configuration file. This is a Python module that contains settings as module-level variables. Here’s an example with some essential settings:

Read more...

Preorder My New Book: Boost Your Django DX

Developer Experience (DX) is a catch-all term for anything that can improve your development workflow. Such improvements can help you write better code, faster, with fewer bugs.

Read more...

Python type hints: types for a descriptor

The descriptor protocol allow us to completely customize attribute access. Python’s documentation describes the protocol with types involved described with words. Let’s look at how we can write those as type hints.

Read more...

A Python Script Template with Sub-commands (and Type Hints)

Earlier this week I shared my Python script template. Here’s an extended version with sub-command support, and an example script.

Read more...

How to Create a Transparent Attribute Alias in Python

When dealing with evolvng APIs, it may be useful to rename an attribute in a class, but keep the old name around for backwards compatibility. This would mean making one attribute an alias for another. In this post we’ll look at two ways to achieve this.

Read more...

Three more uses for functools.partial() in Django

I remain convinced that Python’s functools.partial() is underappreciated. Following my previous post, here are three more ways to use partial() with Django.

Read more...

The Many Ways to Exit in Python

It’s fundamentally useful to exit your program when it’s done. Here are five(!) ways to do so in Python.

Read more...