typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

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

Open oprypin opened this issue 1 year ago • 4 comments

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

oprypin avatar Oct 16 '24 13:10 oprypin

Feel free to submit a PR!

And if you're motivated to get this to a better state, consider writing a PEP for something like python/typing#1096.

JelleZijlstra avatar Oct 16 '24 13:10 JelleZijlstra

Or python/typing#566, which would replace this with AnyOf[X, None] and could also be controlled with strictness flags.

srittau avatar Oct 16 '24 13:10 srittau

Sure, if AnyOf is ever approved, then in this repository one could find all occurrences of MaybeNone and replace them.

But that's what I want to track in this issue - that all the occurrences are readily marked for this. So that then it's more straightforward to apply any such ideas.

oprypin avatar Oct 16 '24 14:10 oprypin

But not sure whether I'll actually send PRs for this

oprypin avatar Oct 16 '24 14:10 oprypin

@JelleZijlstra Looks like this issue can be closed since the PR: https://github.com/python/typeshed/pull/12855 was merged.

kbaikov avatar Dec 17 '24 21:12 kbaikov

That PR is only for stdlib, I haven't looked at stubs of third party libraries

oprypin avatar Dec 17 '24 21:12 oprypin