Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Fix missing type store for overloads#16803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
hauntsaninja merged 1 commit intopython:masterfromcdce8p:fix-type-store
Jan 31, 2024

Conversation

@cdce8p
Copy link
Collaborator

Add missing call to store inferred types if an overload match is found early. All other code paths already do that.

Some background on the issue this fixes

I recently saw an interesting pattern inaiohttp to type values in andict[str, Any] by subclassing dict.

T=TypeVar("T")U=TypeVar("U")classKey(Generic[T]):    ...classCustomDict(dict[Key[Any]|str,Any]):@overload# type: ignore[override]defget(self,__key:Key[T])->T|None:        ...@overloaddefget(self,__key:Key[T],__default:U)->T|U:        ...@overloaddefget(self,__key:str)->Any|None:        ...@overloaddefget(self,__key:str,__default:Any)->Any:        ...defget(self,__key:Key[Any]|str,__default:Any=None)->Any:"""Forward to super implementation."""returnsuper().get(__key,__default)# overloads for __getitem__, setdefault, pop# ...@overload# type: ignore[override]def__setitem__(self,key:Key[T],value:T)->None:        ...@overloaddef__setitem__(self,key:str,value:Any)->None:        ...def__setitem__(self,key:Key[Any]|str,value:Any)->None:"""Forward to super implementation."""returnsuper().__setitem__(key,value)

With the exception that these overloads aren't technically compatible with the supertype, they do the job.

d=CustomDict()key=Key[int]()other_key="other"assert_type(d.get(key),int|None)assert_type(d.get("other"),Any|None)

The issue exists for the__setitem__ case. Without this PR the following would create an issue. Herevar would be inferred asdict[Never, Never], even though it should bedict[Any, Any] which is the case for non-subclassed dicts.

defa2(d:CustomDict)->None:if (var:=d.get("arg"))isNone:var=d["arg"]= {}reveal_type(var)

@github-actions
Copy link
Contributor

According tomypy_primer, this change doesn't affect type check results on a corpus of open source code. ✅

@JelleZijlstraJelleZijlstra self-requested a reviewJanuary 24, 2024 21:11
Copy link
Collaborator

@hauntsaninjahauntsaninja left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others.Learn more.

Nice, thanks!

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

Reviewers

@hauntsaninjahauntsaninjahauntsaninja approved these changes

@JelleZijlstraJelleZijlstraAwaiting requested review from JelleZijlstra

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

2 participants

@cdce8p@hauntsaninja

[8]ページ先頭

©2009-2025 Movatter.jp