typeshed icon indicating copy to clipboard operation
typeshed copied to clipboard

Make Counter generic over the value

Open srittau opened this issue 1 year ago • 18 comments

Closes: #3438

srittau avatar Mar 20 '24 04:03 srittau

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Mar 20 '24 04:03 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Mar 20 '24 06:03 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Mar 20 '24 06:03 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Mar 20 '24 06:03 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 07 '24 13:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 07 '24 13:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 07 '24 13:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 09 '24 09:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 09 '24 09:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 09 '24 09:04 github-actions[bot]

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 09 '24 09:04 github-actions[bot]

@erictraut: I'm unsure why pyright fails the test case as written: https://github.com/python/typeshed/actions/runs/8613668710/job/23605460935?pr=11632

custom_c = cast("Counter[str, Foo]", Counter())
# [...]
custom_c["a"] += 42  # type: ignore

pyright claims that the type ignore is unnecessary here, but unless I'm missing something, both __getitem__() and __setitem()__ are annotated to require a Foo instance, not int in this case.

srittau avatar Apr 09 '24 09:04 srittau

I'm not able to repro the problem when I copy and paste your modified definition of Counter along with the minimal test case snippet. When I do that, pyright generates an error for custom_c["a"] += 42 as I would expect. So there must be something else going on with your test if you're not seeing this error.

erictraut avatar Apr 09 '24 15:04 erictraut

I remember that this is at least the second time, where a pyright "bug" can't be reproduced outside our CI environment. Something strange is going on with our CI it seems.

srittau avatar Apr 09 '24 15:04 srittau

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

github-actions[bot] avatar Apr 14 '25 09:04 github-actions[bot]

The weird CI problems are gone now. Ready to review.

srittau avatar Apr 14 '25 09:04 srittau

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

materialize (https://github.com/MaterializeInc/materialize)
+ misc/python/materialize/parallel_workload/parallel_workload.py:326: error: Argument 1 to "defaultdict" has incompatible type "type[Counter[_T, _C]]"; expected "Callable[[], Counter[Never, Never]] | None"  [arg-type]

github-actions[bot] avatar May 16 '25 16:05 github-actions[bot]