-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
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
Class with function fields incorrectly thinks first argument is self #5485
Comments
This is not something specific to dataclasses. There is a simpler repro that gives same errors: class Repro:
fn: Callable[[int], str]
r = Repro()
r.fn(1) The problem is the same as in #708. I would however not close this as a duplicate, because this issue highlights another aspect of the problem. |
I just came across the same issue. I guess a lot of people are going to face this issue very soon, as it's very easy to reproduce it using dataclasses. |
I guess this should be not hard to fix, mypy cares whether a callable was assigned in class (to match what Python does), so we should just tweak the |
If someone needs a workaround, this works: from dataclasses import dataclass
from typing import *
T = TypeVar("T")
@dataclass
class Box(Generic[T]):
inner: T
@property
def unboxed(self) -> T:
return self.inner
@dataclass
class Repro:
fn: Box[Callable[[int], str]]
Repro(fn=Box(repr)).fn.unboxed(1) |
I've found that another workaround to this is to use a callback protocol instead of |
When using NamedTuple instead of dataclass this problem does not arise. |
I volunteer to fix this and any guidances will be greatly helpful @ilevkivskyi |
@TH3CHARLie Fixing this in general may be tricky, but fixing just the dataclass case, should be easy. In any case, please go ahead! |
commit a2887b7 Author: Jacob Oscarson <jacob@414soft.com> Date: Fri Jun 12 11:02:50 2020 +0200 Polish commit 5f78d1f Author: Jacob Oscarson <jacob@414soft.com> Date: Thu Jun 11 17:34:12 2020 +0200 More test coverage, clean up commit 94a3842 Author: Jacob Oscarson <jacob@414soft.com> Date: Thu Jun 11 17:31:56 2020 +0200 Small typing fixes commit 0d0f9c9 Author: Jacob Oscarson <jacob@414soft.com> Date: Thu Jun 11 17:28:25 2020 +0200 No coverage on kind.py until problems due to python/mypy#5485 is ok
This comment has been minimized.
This comment has been minimized.
I'm still running into this issue with dataclasses. @TH3CHARLie @ilevkivskyi Any resolution on this? 😃 versionsmypy 0.782 |
@jordan-bucholtz i don't know if it makes any difference but fyi there is 0.800 (and 0.790 in between). |
@altendky I'm using 0.800, and this is still broken for me. |
Wondering if the solution is to somehow flag the Callable instance variable as a static method. Because that's essentially the intended use. Now, I'm not sure if the fix should be in mypy or in the typing library. |
We're not ignoring it just because we don't like it, but because mypy is having a hard time dealing with a callback's type. See python/mypy#5485 Signed-off-by: Joao Eduardo Luis <joao@suse.com>
We're not ignoring it just because we don't like it, but because mypy is having a hard time dealing with a callback's type. See python/mypy#5485 Signed-off-by: Joao Eduardo Luis <joao@suse.com>
We're not ignoring it just because we don't like it, but because mypy is having a hard time dealing with a callback's type. See python/mypy#5485 Signed-off-by: Joao Eduardo Luis <joao@suse.com>
The newer version includes a partial fix for python/mypy#5485 (it doesn't fix the problem for frozen dataclasses, but fixes other cases).
Probably too late for @nh2, but for anyone else who could use an example: from dataclasses import dataclass
from typing import Callable
from typing_extensions import Protocol
class FunctionType(Protocol):
def __call__(self, x: int) -> int:
pass
@dataclass(frozen=True)
class A:
fn: Callable[[int], int]
@dataclass(frozen=True)
class B:
fn: FunctionType
A(fn=lambda x: x).fn(1) # Invalid self argument "A" to attribute function "fn" with type "Callable[[int], int]"
B(fn=lambda x: x).fn(1) |
This is really just for MyPy, it was triggering python/mypy#5485
I encoutered this with
This way I can't use any of the workarounds because I can't modify the standard |
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
Fixes python#708 and Fixes python#5485 Prevent handling as bounded method of callable members declared as instance variables.
Fixes #708 Fixes #5485 This builds on the original proposal, but handles three important issues/edge cases: * This PR fixes serialization of `is_inferred` so that the distinction works correctly in incremental mode (I added a test) * Dunder operator methods are always considered class variables (this is a relatively common pattern and matches Python semantics; there is an existing tests that previously needed `ClassVar[...]`) * If we detect a `Too few arguments` error for a variable with callable type we give a note suggesting to try `ClassVar[...]` I also add a short doc paragraph on this. Co-authored-by: wyfo <joperez@hotmail.fr>
Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com>
* Reflect the deprecation of get_response being None. Signed-off-by: Zixuan James Li <p359101898@gmail.com> * Type get_response with a callback protocol. Otherwise, calling `self.get_response(request)` in a subclass of `MiddlewareMixin` runs into `Invalid self argument` error. This is a workaround for python/mypy#5485. Signed-off-by: Zixuan James Li <p359101898@gmail.com> Signed-off-by: Zixuan James Li <p359101898@gmail.com>
This is a bug report.
Minimal repro
Works correctly when dataclass is not used, i.e. when
Repro
isActual behavior
When running with Python, works correctly. Doesn't throw any exceptions.
Expected behavior
Successful type check
Environment
Versions
Tested with two versions, 0.620 and current version from GitHub.
Mypy config
The text was updated successfully, but these errors were encountered: