Coder Social home page Coder Social logo

Comments (12)

benbariteau avatar benbariteau commented on July 28, 2024 2

I also ran into this issue.

from typing_extensions.

srittau avatar srittau commented on July 28, 2024

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.

JelleZijlstra avatar JelleZijlstra commented on July 28, 2024

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.

srittau avatar srittau commented on July 28, 2024

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.

V1NAY8 avatar V1NAY8 commented on July 28, 2024

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.

gvanrossum avatar gvanrossum commented on July 28, 2024

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.

hauntsaninja avatar hauntsaninja commented on July 28, 2024

@V1NAY8

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... your try_sort expects to raise a TypeError sometimes, which is naturally something that mypy should complain about! So I would continue to use your Item TypeVar and just type: ignore the sorted 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 need try_sort and can just use sorted everywhere.

@everyoneelse

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.

guilhermeleobas avatar guilhermeleobas commented on July 28, 2024

@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.

hauntsaninja avatar hauntsaninja commented on July 28, 2024

@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 do cast(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.

guilhermeleobas avatar guilhermeleobas commented on July 28, 2024

Thanks @hauntsaninja, I will give it a try.

from typing_extensions.

brettcs avatar brettcs commented on July 28, 2024

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.

JelleZijlstra avatar JelleZijlstra commented on July 28, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.