python / python/typeshed

Consistently mark types with `| MaybeNone` whenever it is applicable

Open
#12,822 6 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
5.1k
Forks
2.1k
Avg merge
1d 19h
Merged PRs (30d)
82

Description

I noticed that there's a special type alias MaybeNone that represents this specific intent:

We didn't dare to change the type to X | None so instead we changed it to Any

You can find an example in a relatively recent addition of this.

And while this approach makes me very sad, it is good that such cases are marked with MaybeNone, for the purpose of looking at them specially. Maybe some type checker will arise that will have specific handling for it and do something smarter instead.

With that in mind, it would be good to ensure that all such cases are consistently marked.

I noticed at least one case that is not marked like this. It has | Any directly written instead of | MaybeNone. This is in re.match.group*:

https://github.com/python/typeshed/blob/6feca188689e14cdacd8dbf8bc9cf0635993027a/stdlib/re.pyi#L93-L117

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with stdlib/re.pyi, especially the re.match.group* definitions around lines 93–117, and compare them with the MaybeNone alias in stdlib/_typeshed/init.pyi. Review similar cases across the stubs where this intent is represented with Any; done means applicable cases consistently use MaybeNone instead, with no unmarked exceptions found.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.