GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — GitHub - Kludex/no-optional: Replace `Optional[T]` by `Union[T, None]` 👀

BranchesTags
Open more actions menu

Latest commit

 

History

22 Commits
22 Commits

Folders and files

NameName
Last commit message
Last commit date
.github
.github
 
 
no_optional
no_optional
 
 
tests
tests
 
 
.gitignore
.gitignore
 
 
.pre-commit-config.yaml
.pre-commit-config.yaml
 
 
.pre-commit-hooks.yaml
.pre-commit-hooks.yaml
 
 
LICENSE
LICENSE
 
 
Makefile
Makefile
 
 
README.md
README.md
 
 
pyproject.toml
pyproject.toml
 
 
setup.cfg
setup.cfg
 
 
setup.py
setup.py
 
 

Repository files navigation

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

Alternative

pyupgrade is great but no-optional is better when you need runtime support like for FastAPI and pydantic.

The reason being that no-optional just does the replacement. On the other hand, pyupgrade requires from __future__ import annotations for versions below Python 3.10.

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.

About

Replace `Optional[T]` by `Union[T, None]` 👀

Resources

Readme
MIT license

Contributing

Contributing

Security policy

Security policy
Activity

Stars

57 stars

Watchers

2 watching

Forks

0 forks
Report repository

Releases

Used by

Contributors

Languages

You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.