-
-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make Counter generic over the value #11632
Open
srittau
wants to merge
17
commits into
python:main
Choose a base branch
from
srittau:generic-counter
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+59
−20
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
e7e430a
Make Counter generic over the value
srittau fc7af55
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] ab7f64c
Replace some more ints, fix a test
srittau 44ed688
Add generic arguments
srittau 923d8e2
Add more tests
srittau f460e39
Disable a test
srittau 424d3c0
Make pyright happy
srittau e336926
pyright is quite unhappy today ...
srittau 2f5105b
...
srittau dcf86cf
Improve tests
srittau caa5b14
Fix test
srittau e10c23c
Revert changes that make pyright unhappy
srittau b107d20
Use explicit cast in test cases
srittau 4723e02
Add a test
srittau 39aa741
Try using `Any` in type assertion
srittau f9a14ba
Fix test
srittau 3fe47e0
Streamline tests
srittau File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
from __future__ import annotations | ||
|
||
from collections import Counter | ||
from typing import Any, cast | ||
from typing_extensions import assert_type | ||
|
||
|
||
class Foo: ... | ||
|
||
|
||
# Test the constructor | ||
# mypy derives Never for the first type argument while, pyright derives Unknown | ||
assert_type(Counter(), "Counter[Any, int]") | ||
assert_type(Counter(foo=42.2), "Counter[str, float]") | ||
assert_type(Counter({42: "bar"}), "Counter[int, str]") | ||
assert_type(Counter([1, 2, 3]), "Counter[int, int]") | ||
|
||
int_c: Counter[str] = Counter() | ||
assert_type(int_c, "Counter[str, int]") | ||
assert_type(int_c["a"], int) | ||
int_c["a"] = 1 | ||
int_c["a"] += 3 | ||
int_c["a"] += 3.5 # type: ignore | ||
|
||
float_c = Counter(foo=42.2) | ||
assert_type(float_c, "Counter[str, float]") | ||
assert_type(float_c["a"], float) | ||
float_c["a"] = 1.0 | ||
float_c["a"] += 3.0 | ||
float_c["a"] += 42 | ||
float_c["a"] += "42" # type: ignore | ||
|
||
custom_c = cast("Counter[str, Foo]", Counter()) | ||
assert_type(custom_c, "Counter[str, Foo]") | ||
assert_type(custom_c["a"], Foo) | ||
custom_c["a"] = Foo() | ||
custom_c["a"] += Foo() # type: ignore | ||
custom_c["a"] += 42 # type: ignore | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
At runtime this is actually an int though. I wonder if we need to make all these methods return
_C | int
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line should probably not be accepted, as
Counter()
is aCounter[unknown, int]
, which is incompatible withCounter[..., Foo]
. I'm not sure the test makes much sense.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doesn't this sort of problem apply to any Counter with a non-int value type, though? This seems like a fundamental problem with this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder whether type checkers support
__missing__
, in which case, this should happen automatically when we add it to the stubs. But returning_C | int
makes some sense to me for getter methods.