Comments (2)
To add a bit more information, the same example works fine if the I
type is included within the P
param spec (see that in the playground).
However, being able to extract a type from the param spec as seen in the first code sample is really useful. In particular, the type of the first argument might appear in other methods of the protocol, for instance:
# An object that can convert I to O with parameters P
class ConvertorProtocol(Protocol[I, P, O]):
def convert(self, __source: I, *args: P.args, **kwargs: P.kwargs) -> O:
...
def prepare(self, *args: P.args, **kwargs: P.kwargs) -> Callable[[I], O]:
...
from mypy.
I just came up with a simpler example that can reproduce the regression, without Protocol
nor decorators:
from typing import (
Generic,
TypeVar,
Callable,
Concatenate,
ParamSpec,
reveal_type,
)
from dataclasses import dataclass
X = TypeVar("X")
P = ParamSpec("P")
I = TypeVar("I", contravariant=True)
O = TypeVar("O", covariant=True)
@dataclass
class Convertor(Generic[I, P, O]):
convert: Callable[Concatenate[I, P], O]
def as_list(source: X, repeat: int = 1) -> list[X]:
return [source] * repeat
as_list_convertor = Convertor(convert=as_list)
if __name__ == "__main__":
reveal_type(as_list_convertor.convert)
result = as_list_convertor.convert(1, repeat=3)
reveal_type(result)
assert result == [1, 1, 1]
You can see in the playground that it passes with mypy 1.6 but starts failing with mypy 1.7.
from mypy.
Related Issues (20)
- `__eq__` methods disable narrowing in the `else` block
- match narrowing fails when the operand is an `await` expression
- Slow typechecking on nested TypedDict with union members HOT 1
- Attempted to reuse member name in Enum definition
- Segmentation fault with recursive type
- Call `tuple` on a known length `tuple[int, int]` would lose its length HOT 2
- Set and Dict generalizing the type unnecessarily
- Unpacking doesn't do proper type narrowing
- Support `Unpack` with `dict` for keyword arguments: `**kwargs: Unpack[dict[Literal["test"], ...]]` HOT 5
- Dataclasses: `No overload variant of "asdict" matches argument type "Self" [call-overload]` when deciding decorator conditionally
- Feature Request: a secondary error for overrides based on compatibility
- Mypy handling initializing Pydantic models with dict values incorrectly HOT 2
- no error when using generic in `except` block
- `mypy` crashes with `KeyError: 'type_is'` HOT 2
- overloaded __new__ ignored when introducing __init__
- Exhaustiveness checking false positive regression in 1.10 HOT 1
- Support for short-circuit evaluation HOT 8
- mypy does not check the code after the line with lambda function that calls NoReturn function HOT 8
- Base class type incorrectly overwritten by assignment in extending class HOT 1
- Disable third party imports only 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 mypy.