| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
…ition to typevar unions
Sometimes our constraints builder needs to express "at least one of
these constraints must be true". Sometimes it's trivial, but sometimes
they have nothing in common - in that case we fall back to forgetting
all of them and (usually) inferring `Never`.
This PR extends the fallback handling logic by one more rule: before
giving up, we try restricting the constraints to meta variables only.
This means that when we try to express `T :> str || U :> str` in the
following setup:
```
from typing import TypeVar
T = TypeVar("T")
U = TypeVar("U")
class Foo(Generic[T]):
def func(self, arg: T | U) -> None: ...
```
we won't ignore both and fall back to `Never` but will pick `U :> str`
instead. The reason for this heuristic is that function-scoped typevars
are definitely something we're trying to infer, while everything else is
usually out of our control, any other type variable should stay intact.
There are other places where constraint builder may emit restrictions on
type variables it does not control, handling them consistently
everywhere is left as an exercise to the reader.
This isn't safe in general case - it might be that another typevar
satisfies the constraints but the chosen one doesn't. However, this
shouldn't make any existing inference worse: if we used to infer `Never`
and it worked, then anything else should almost definitely work as well.
See the added testcase for motivation: currently `mypy` fails to handle
`Mapping.get` with default without return type context when `Mapping`
has a type variable as the second argument.
https://mypy-play.net/?mypy=1.15.0&python=3.12&flags=strict&gist=2f9493548082e66b77750655d3a90218
This is a prerequisite of #18976 - that inference change makes the
problem solved here occur more often.
|
Okay, this is clearly an improvement - xarray case is a regression, but all other hits are either fixed false positives or improved error messages. |
Sorry, something went wrong.
|
Diff from mypy_primer, showing the effect of this PR on open source code: xarray (https://github.com/pydata/xarray)
+ xarray/conventions.py: note: In function "decode_cf_variables":
+ xarray/conventions.py:410: error: Argument "decode_times" to "decode_cf_variable" has incompatible type "object"; expected "bool | CFDatetimeCoder" [arg-type]
antidote (https://github.com/Finistere/antidote)
- src/antidote/lib/interface_ext/_provider.py:313: error: Generator has incompatible item type "NewWeight"; expected "Weight" [misc]
- src/antidote/lib/interface_ext/_provider.py:314: error: Argument 2 to "sum" has incompatible type "NewWeight"; expected "Weight" [arg-type]
+ src/antidote/lib/interface_ext/_provider.py:312: error: Argument "weight" to "replace" of "CandidateImplementation[Weight]" has incompatible type "NewWeight"; expected "Weight" [arg-type]
operator (https://github.com/canonical/operator)
- ops/_main.py:97: error: Argument 2 to "next" has incompatible type "None"; expected "Storage" [arg-type]
+ ops/_main.py:97: error: Incompatible types in assignment (expression has type "Storage | None", variable has type "Storage") [assignment]
setuptools (https://github.com/pypa/setuptools)
+ setuptools/msvc.py:1442: error: Unused "type: ignore" comment [unused-ignore]
Tanjun (https://github.com/FasterSpeeding/Tanjun)
- tanjun/clients.py:2428: error: Argument 1 to "get_type_dependency" of "Client" has incompatible type "type[SingleStoreCache[AuthorizationApplication]]"; expected "type[SingleStoreCache[Application]] | type[None]" [arg-type]
werkzeug (https://github.com/pallets/werkzeug)
+ src/werkzeug/datastructures/mixins.py:280: error: Unused "type: ignore" comment [unused-ignore]
+ src/werkzeug/datastructures/mixins.py:280: error: Incompatible types in assignment (expression has type "Union[V, T]", variable has type "V") [assignment]
+ src/werkzeug/datastructures/mixins.py:280: note: Error code "assignment" not covered by "type: ignore" comment
archinstall (https://github.com/archlinux/archinstall)
+ archinstall/lib/profile/profiles_handler.py:171: error: Unused "type: ignore[arg-type, union-attr]" comment [unused-ignore]
|
Sorry, something went wrong.
There was a problem hiding this comment.
Nice, many common functions with this shape!
Sorry, something went wrong.
There was a problem hiding this comment.
Yeah, this is a net improvement.
Sorry, something went wrong.
|
Btw @hauntsaninja what does "upnext" label mean? Can I merge this PR now? |
Sorry, something went wrong.
|
Yeah we can merge! It means "I want to make sure this PR is not forgotten forever in case no one else gets to it" |
Sorry, something went wrong.
This commit also removes a type ignore comment that is no longer needed after python/mypy#18976.
This commit also removes a type ignore comment that is no longer needed after python/mypy#18976.
| Back | FazBrowse Home | New Git URL |
When using context, we can perform some overly optimistic inference when return type is T1 | T2. This breaks in important case of builtins.min when default and key are passed, essentially making them always incompatible. This is not the most principled approach, but let's see the primer results.
This resolves quite a few issues (some of them duplicates, but some - substantially different), min problem was a very popular one... Diff run: sterliakov/mypy-issues#30