Skip to main content

Replace `Optional[T]` by `Union[T, None]`! 🎉

Project description

no-optional

Package version

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.

image

As the tweet says, we have two reasons for doing this:

  1. It's more explicit to write Union[str, None] than Optional[str]. Mainly because Optional[str] doesn't mean that the attribute is optional. It only means that it accepts None as a possible value.
  2. On Python 3.10+ you can type annotate as str | None instead of the above two. Which is more similar to Union[str, None] than Optional[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.4.0
    hooks:
      - id: no_optional

License

This project is licensed under the terms of the MIT license.

Project details


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

no_optional-0.4.0-py3-none-any.whl (6.8 kB view details)

Uploaded Python 3

File details

Details for the file no_optional-0.4.0-py3-none-any.whl.

File metadata

  • Download URL: no_optional-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 6.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.8.12

File hashes

Hashes for no_optional-0.4.0-py3-none-any.whl
Algorithm Hash digest
SHA256 d4a625f7abbbaeef3b77ed88f53a44134c6f2d5a365378bb4a0197d94b95c998
MD5 adb551020e7b385aab619005b4450e2a
BLAKE2b-256 9a64b6d3d2e446c7c0b7d815359c2991724253be769a4e744ae4658a3caf8540

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page