-
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Use generic defaults for Counter
value
#12344
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
Open
max-muoto
wants to merge
18
commits into
python:main
Choose a base branch
from
max-muoto:generics-for-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.
Open
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
8790f72
Use generic defaults for Counter
max-muoto 597a528
Avoid runtime issues
max-muoto 6a4086e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] a7101e8
Redundant generic
max-muoto e297103
Test fixes
max-muoto 2cbc94b
Fix issues
max-muoto 10b1117
Appease mypy
max-muoto 081ddb1
Fix
max-muoto 15b3bad
kwargs should be V_I
max-muoto 1fb714c
update fromkeys
max-muoto aa66813
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 9086420
Merge branch 'main' into generics-for-counter
max-muoto 24c46e3
Tweak
max-muoto 68d3198
Expand tests
max-muoto 64667cb
Fix edge case
max-muoto 8a9ddd1
Overrides
max-muoto a045c3c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] 2a3701c
Avoid pipe operator
max-muoto 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from __future__ import annotations | ||
|
||
from collections import Counter | ||
from decimal import Decimal | ||
from typing import Union | ||
from typing_extensions import assert_type | ||
|
||
# Initialize a Counter for strings with integer values | ||
word_counts: Counter[str] = Counter() | ||
word_counts["foo"] += 3 | ||
word_counts["bar"] += 2 | ||
assert_type(word_counts, "Counter[str, int]") | ||
|
||
# Initialize a Counter for strings with float values | ||
floating_point_counts: Counter[str, float] = Counter() | ||
floating_point_counts["foo"] += 3.0 | ||
floating_point_counts["bar"] += 5.0 | ||
|
||
# Initialize a Counter for strings with Decimal values | ||
decimal_counts: Counter[str, Decimal] = Counter() | ||
decimal_counts["foo"] += Decimal("3.0") | ||
decimal_counts["bar"] += Decimal("5.0") | ||
# Each key defualts to an int. | ||
assert_type(decimal_counts["test"], Union[Decimal, int]) | ||
assert_type(decimal_counts.get("test"), Union[Decimal, int, None]) | ||
assert_type(decimal_counts.pop("test"), Union[Decimal, int]) | ||
|
||
# Using kwargs for `__init__` | ||
word_counts = Counter(foo=3, bar=2) | ||
assert_type(word_counts, "Counter[str, int]") | ||
floating_point_counts = Counter(foo=3.0, bar=5.0) | ||
assert_type(floating_point_counts, "Counter[str, float]") | ||
decimal_counts = Counter(foo=Decimal("3.0"), bar=Decimal("5.0")) | ||
assert_type(decimal_counts, "Counter[str, Decimal]") | ||
|
||
# Counter combining integers and floats | ||
mixed_type_counter = Counter({"foo": 3, "bar": 2.5}) | ||
mixed_type_counter["baz"] += 1.5 | ||
mixed_type_counter # pyright: ignore[reportUnusedExpression] # mypy: `"Counter[str, float]"`; pyright: `Counter[str, int | float]` | ||
|
||
# Check ORing and ANDing Counters with different value types | ||
# MyPy and Pyright infer the types differently for these, so we can't use assert_type. | ||
|
||
_ = mixed_type_counter or decimal_counts | ||
_ # pyright: ignore[reportUnusedExpression] # mypy: `"Counter[str, float] | Counter[str, Decimal]"`; pyright: `Counter[str, int | float] | Counter[str, Decimal]` | ||
|
||
_ = decimal_counts or mixed_type_counter | ||
_ # pyright: ignore[reportUnusedExpression] # mypy: `"Counter[str, Decimal] | Counter[str, float]"`; pyright: `Counter[str, Decimal] | Counter[str, int | float]` | ||
|
||
_ = mixed_type_counter and decimal_counts | ||
_ # pyright: ignore[reportUnusedExpression] # mypy: `"Counter[str, float] | Counter[str, Decimal]"`; pyright: `Counter[str, int | float] | Counter[str, Decimal]` | ||
|
||
_ = decimal_counts and mixed_type_counter | ||
_ # pyright: ignore[reportUnusedExpression] # mypy: `"Counter[str, Decimal] | Counter[str, float]"`; pyright: `Counter[str, Decimal] | Counter[str, int | float]` | ||
|
||
# We shouldn't be able to add Counters with incompatible value types | ||
_ = mixed_type_counter + decimal_counts # type: ignore | ||
mixed_type_counter += decimal_counts # type: ignore | ||
|
||
# Adding Counters with compatible types | ||
_wc = word_counts + Counter({"foo": 2, "baz": 1}) | ||
word_counts += Counter({"foo": 2, "baz": 1}) | ||
|
||
# Combining Counters of different key types | ||
integer_key_counter = Counter({1: 2, 2: 3}) | ||
combined_word_and_integer_keys = word_counts + integer_key_counter | ||
assert_type(combined_word_and_integer_keys, "Counter[str | int, int]") |
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
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.
We might prefer
Literal[0]
here, curious what we think.