-
Notifications
You must be signed in to change notification settings - Fork 242
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
Counter.__init__
can't be typed safely
#1846
Comments
Perhaps this is because in general, Does it cause a performance hit to make a StrictCounter subclass that can only have integer values? In my opinion, the goal of Python typing is not to type everything. It's a tool, to help you avoid bug prone dynamic or none type-safe code. Therefore by not easily typing this example (without some hack or other), the type system is working as intended. By only letting this code pass typechecking -- by not adding any type hints at all to it -- the type system has highlighted to you the author, a part of your code that is prone to bugs - assigning arbitrary key values to a Counter. Perhaps this would be accomplished more smoothly and more easily understandable, via something like a version of the Coverage tool for testing, but for type hints. It seems that historically, it was decided to make Counter a If you disagree then please could you expand on what the intention is behind adding non-integer values to a key/value item in a Counter? And if there is a good use case, what need is there to make such a dynamic use case type safe (or a use case that's only superficially, loosely type-able - e.g. making eveyrthing |
Code does not need type annotations in order to be fully and completely typed. There is no You can add type annotations and it doesn't change anything: a: dict[str, str] = {"a": "hello"}
b: Counter[str] = Counter(a)
c: int = b.get("a", 0)
print(c + 2) # crash The type system does not allow adding strings to the values of a |
Well OK, if there's already special code in type checkers to support If there's a desire to implement that by people that want to, then good for them. Have at it, Otherwise it's reasonable to conclude this crash is due to Python's dynamic typing, and can't be checked for without simply trying it at run time. Python and its type system provides users that want to initialise a Counter[str] from a dict[str,str] with plenty of other tools that could fix this for themselves, e.g. making their own better-typed interface layer. |
I don't know what you're suggesting that anyone implement here. The problem is that the typeshed typing for |
Counter
overloads are unsafeand I think there isn't a way to express them safely in the current type system.
Iterable[_T]
needs to be something likeIterable[_T] - Mapping[_T, ~int]
If type checkers want to address this now, it would probably have to be a special case.
Without having to worry about type intersections/differences/negations, maybe we could have a way to indicate:
"If this overload matches (after all previous overloads fail), a type error should be reported."
something like:
The text was updated successfully, but these errors were encountered: