Comments (12)
I also ran into this issue.
from typing_extensions.
Sounds like a good idea. In fact, adding protocols for other dunder protocols makes sense, too. See for example the dunder protocols in builtins.
from typing_extensions.
Adding it to typing(_extensions) is relatively painful though because it ties us to Python's release cycle. An alternative could be to release our _typeshed
stub-only module as a PyPI package with a runtime component. On the plus side for adding these things to typing
though, it's more discoverable for users than a new package.
from typing_extensions.
Oops, I was misreading this. I was talking about _typeshed
, not typing(_extensions)
, although I'm +0 to add it to the latter as well.
from typing_extensions.
The Example given above was this
Item = TypeVar("Item")
def try_sort(iterable: Iterable[Item]) -> Iterable[Item]:
# Pulled from pandas.core.common since
# it was deprecated and removed in 1.1
listed = list(iterable)
try:
return sorted(listed)
except TypeError:
return listed
- mypy says:
Value of type variable "_LT" of "sorted" cannot be "Item"
So, I changed it to str and it worked 😮
def try_sort(iterable: Iterable[str]) -> Iterable[str]:
# Pulled from pandas.core.common since
# it was deprecated and removed in 1.1
listed = list(iterable)
try:
return sorted(listed)
except TypeError:
return listed
I am trying to figure out what was the general solution to this issue?
from typing_extensions.
Adding it to typing(_extensions) is relatively painful though because it ties us to Python's release cycle.
Note that typing_extensions is not tied to the CPython release cycle.
from typing_extensions.
The general solution if we add this to typing(_extensions)
is:
SupportsLessThanT = typing.TypeVar("SupportsLessThanT", bound=typing.SupportsLessThan)
def function_that_wraps_sort(iterable: SupportsLessThanT) -> SupportsLessThanT: ...
If this isn't added to typing(_extensions)
, you'll need to define the SupportsLessThan
protocol yourself , which looks like this.
But now that I'm looking closely at your code, some more comments:
- What mypy does now is try to ensure that arguments to sorted are comparable. The above "general" solution will try to ensure that arguments to
try_sort
are comparable. But that doesn't seem to be what you want... yourtry_sort
expects to raise a TypeError sometimes, which is naturally something that mypy should complain about! So I would continue to use yourItem
TypeVar and justtype: ignore
thesorted
line, as you would if you were doing:try: 1 + sometimes_int_sometimes_str() except: TypeError: ...
- In your case, if you can change the type to
Iterable[str]
without mypy complaining, it might mean you don't actually needtry_sort
and can just usesorted
everywhere.
It sounds like no one is opposed, but no one feels strongly either? In that case, maybe we hold off and see if people complain, rather than presumptively making a decision based on mypy_primer.
It's also not clear to me what, if any, the process here is.
from typing_extensions.
@hauntsaninja, I am facing the same problem with min
:
torch/distributions/kl.py:112: error: Value of type variable "_LT" of "min" cannot be "_Match" [type-var]
torch/distributions/kl.py:113: error: Value of type variable "_LT" of "min" cannot be "_Match" [type-var]
The lines involved are the ones below
https://github.com/pytorch/pytorch/blob/146721f1dfe29786f5d25a9e2a2c2227dfa85f76/torch/distributions/kl.py#L107-L108
from typing_extensions.
@guilhermeleobas Hmm, your false positive is a little tricky. The problem is that there isn't a good way to type the return type of functools.total_ordering
(python/mypy#4610 / intersection types), so mypy don't see that _Match
does have __lt__
and things are in fact okay.
Some possibilities:
- If we were to add SupportsLessThan to
typing(_extensions)
/ if you define it yourself, you can docast(SupportsLessThan, Match(...)) for m in matches)
- You can use the implementation details of functools.total_ordering to somewhat sketchily add
__lt__: Callable = None # type: ignore
to the_Match
class. - You can type ignore
from typing_extensions.
Thanks @hauntsaninja, I will give it a try.
from typing_extensions.
It sounds like no one is opposed, but no one feels strongly either? In that case, maybe we hold off and see if people complain, rather than presumptively making a decision based on mypy_primer.
I ended up here after upgrading my code from mypy 0.770 to 0.810, and this was the biggest issue I ran into. I have a handful of sort key functions that I had previously annotated as returning Hashable. I understand why Hashable wasn't right, I'm glad that's an error now, but I was a little sad there wasn't an easy low-level type to use in the typing module that I could correctly replace it with.
For me probably the best value I get out of type checking, after catching obvious bugs, is the way it helps document my code. Even though my sort key functions return higher-level types and I could annotate them as such, I like the low-level type annotation as a signal to say "I only promise this return value is comparable against other return values; don't try to do anything else with it." For that reason, I would appreciate an easy way to import "the return type of a sort key function."
from typing_extensions.
Closing this as our current policy is to add only types that are in typing
. #6 proposes changing that.
In addition, typeshed no longer has a SupportsLessThan
type.
from typing_extensions.
Related Issues (20)
- Backport CapsuleType from 3.13
- Python 3.13.0b1: test_typing_extensions: `AttributeError: attribute '__default__' of 'typing.TypeVar' objects is not writable` HOT 3
- Daily tests failed on Sat May 11 2024
- Third-party tests failed on Sat May 11 2024 HOT 5
- Third-party tests failed on Sun May 12 2024 HOT 1
- Third-party tests failed on Mon May 13 2024 HOT 1
- Third-party tests failed on Tue May 14 2024
- Improving the security of typing-extensions HOT 8
- PEP-696 specialisation tests from CPython fail HOT 3
- PEP-696: `AttributeError: attribute '__default__' of 'typing.ParamSpec' objects is not writable` on Python 3.13 HOT 4
- ParamSpec `_set_default` called from `ParamSpec.__new__` raises Attribute Error HOT 2
- Add an `override` decorator to a .pyi file HOT 8
- Add `inspect.get_annotations` backport
- Add `typing.get_type_hints` backport HOT 1
- Pylint raises error saying "default" isn't valid for TypeVar HOT 2
- Third-party tests failed on Sat Jun 01 2024 HOT 2
- `TypeError` on nested `Annotated`s with `typing_extensions==4.12.0`
- Third-party tests failed on Sun Jun 02 2024 HOT 2
- 4.12.1: `pyupgrade --py38-plus` generated patch causes pytest fails HOT 9
- Third-party tests failed on Mon Jun 03 2024 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from typing_extensions.