Fix false positive for sentinel calls with a repr argument by crypt0lith · Pull Request #11896 · microsoft/pyright · GitHub
Skip to content

Fix false positive for sentinel calls with a repr argument - #11896

Open
crypt0lith wants to merge 1 commit into
microsoft:mainfrom
crypt0lith:fix/sentinel-repr-kwd
Open

crypt0lith wants to merge 1 commit into
microsoft:mainfrom
crypt0lith:fix/sentinel-repr-kwd

Conversation

@crypt0lith

Copy link
Copy Markdown
Contributor

Summary

Fixes #11893

Passing repr= to sentinel or typing_extensions.Sentinel reports a false reportCallIssue error. The sentinel then evaluates to Never and can't be used in type expressions.

Root cause

createSentinelType reports sentinelParamCount for any call whose argument list doesn't have exactly one entry. Both stubs declare a keyword-only repr: str | None parameter after the positional name, so a valid repr argument fails the check. The function then returns undefined instead of the sentinel type.

Fix

Require exactly one positional argument, and allow repr as the only keyword argument. Report any other argument as before, and report a repr value not assignable to str | None under reportArgumentType. Drop the typing_extensions.Sentinel dispatch check, since the stub now defines Sentinel as an alias of sentinel.

Test

Remove sentinel2.py. Since #11727, typing_extensions.sentinel resolves to builtins.sentinel on 3.15, and every check in sentinel2.py already exists in sentinel1.py. Switch sentinel1.py from the Sentinel alias to sentinel.

Cases added to sentinel1.py
Case Expected Description
BAD_CALL4 error repr value not str | None
BAD_CALL5 error unknown keyword argument
BAD_CALL6 error unknown keyword argument alongside repr
REPR1 repr keyword: sentinel type, usable in a union annotation and narrowed by is

createSentinelType rejects any call with more than one argument, so
passing the keyword-only repr parameter of sentinel reports an error
and evaluates the sentinel to Never. Require exactly one positional
argument, accept a repr keyword argument, and check its type against
'str | None'. Drop the typing_extensions.Sentinel check, which the
stub now aliases to sentinel.

Fixes microsoft#11893

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Wrong sentinel inference with custom repr

1 participant