Replace `Optional[T]` by `Union[T, None]`! 🎉
Project description
no-optional
This codemod replaces typing.Optional[T]
by typing.Union[T, None]
in the codebase.
Why?
This tool was inspired by a tweet from Sebastián Ramírez (as you see below), and a conversation between us.
As the tweet says, we have two reasons for doing this:
- It's more explicit to write
Union[str, None]
thanOptional[str]
. Mainly becauseOptional[str]
doesn't mean that the attribute is optional. It only means that it acceptsNone
as a possible value. - On Python 3.10+ you can type annotate as
str | None
instead of the above two. Which is more similar toUnion[str, None]
thanOptional[str]
.
Installation
pip install no-optional
Usage
Run the following on the repository you want to format:
python -m no_optional <files>
You can also use the pre-commit. Add the following to your .pre-commit-config.yaml
file:
- repo: https://github.com/Kludex/no-optional
rev: 0.3.0
hooks:
- id: no_optional
License
This project is licensed under the terms of the MIT license.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
No source distribution files available for this release.See tutorial on generating distribution archives.
Built Distribution
Close
Hashes for no_optional-0.3.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 25fe8512d8759db653b8ecb0e880624c4cf0c86d128e4b71ea8d581dada984f6 |
|
MD5 | 697541a351ba3a2bf06a0baf7c663a7c |
|
BLAKE2b-256 | fec490344c313de7d9dea3d68c7abd6edc1f8a69b595acea3d3d5305a25be447 |