FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Improve the accuracy of `(default)dict.__(r)or__` by AlexWaygood · Pull Request #10679 · python/typeshed · GitHub

Repository navigation

Improve the accuracy of (default)dict.__(r)or__ - #10679

Merged
AlexWaygood merged 12 commits into
python:mainfrom
AlexWaygood:dict-dot-or
Sep 8, 2023
Merged

AlexWaygood merged 12 commits into
python:mainfrom
AlexWaygood:dict-dot-or

Conversation

Copy link
Copy Markdown
Member

Fixes #10678

This comment has been minimized.

Copy link
Copy Markdown
Member Author

Oh, one mypy_primer error isn't actually too bad at all. I vote for doing this, in that case!

AlexWaygood marked this pull request as ready for review September 7, 2023 12:13

Akuli commented Sep 7, 2023 •
edited
Loading

Copy link
Copy Markdown
Collaborator

Maybe add a test case? We can't use mypy_primer to ensure we don't break this again, since the improvement doesn't show up in the diff and open-source projects are unlikely to contain CaseInsensitiveDict() | dict() anyway.

Copy link
Copy Markdown
Member Author

Sure, I'll add a test

srittau commented Sep 7, 2023 •
edited
Loading

Copy link
Copy Markdown
Collaborator

And the one primer hit actually shows a genuine problem as using an arbitrary mapping wouldn't work either. This is actually something I wasn't aware of, so +10 for changing these annotations.

This comment has been minimized.

AlexWaygood marked this pull request as draft September 7, 2023 15:41
AlexWaygood marked this pull request as ready for review September 7, 2023 16:23

This comment has been minimized.

Copy link
Copy Markdown
Member Author

I don't understand what's going on with the remaining pyright errors on the test cases I've added. @srittau / @Akuli, do either of you understand why they're happening? Mypy is fine with all the test cases I've added.

This comment has been minimized.

AlexWaygood changed the title Improve the accuracy of some __(r)or__ methods Improve the accuracy of (default)dict.__(r)or__ Sep 7, 2023

srittau commented Sep 7, 2023

Copy link
Copy Markdown
Collaborator

One thing I'd try is using a small custom mapping-like type in the tests instead of os.environ. The latter has a lot going on, with generics and python-specific branches, and I wouldn't be surprised if a typechecker would special-case it due to its importance.

This comment has been minimized.

This comment has been minimized.

Copy link
Copy Markdown
Member Author

Okay, for now I've just commented out the tests that were failing on pyright and left TODO comments. There's definitely a behaviour difference from mypy there -- to me it looks like a bug, but not sure. I'll file an issue with pyright later.

AlexWaygood requested review from Akuli and srittau September 7, 2023 21:52

github-actions Bot commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

Diff from mypy_primer, showing the effect of this PR on open source code:

mkosi (https://github.com/systemd/mkosi)
+ mkosi/run.py:177:11: error: No overload variant of "__or__" of "dict" matches argument type "Mapping[str, str]"  [operator]
+ mkosi/run.py:177:11: note: Possible overload variants:
+ mkosi/run.py:177:11: note:     def __or__(self, dict[str, str], /) -> dict[str, str]
+ mkosi/run.py:177:11: note:     def [_T1, _T2] __or__(self, dict[_T1, _T2], /) -> dict[Union[str, _T1], Union[str, _T2]]

Akuli left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

At first glance the tests seemed like too much copy/pasta, but I think it's fine because dict and defaultdict behave differently.

Copy link
Copy Markdown
Member Author

At first glance the tests seemed like too much copy/pasta, but I think it's fine because dict and defaultdict behave differently.

Yeah, dict.__or__ always returns dict, whereas defaultdict.__or__ returns Self. I considered putting all the tests into a check_pep584.py file, but thought it would be too confusing to have some of the tests for dict in a file that had a name that didn't start with check_dict.

This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
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.

False negatives with dict.__(r)or__

3 participants


Back | FazBrowse Home | New Git URL