Skip to content
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

mypy complains about monkey-patching classes in another module #1152

Closed
timabbott opened this issue Jan 25, 2016 · 7 comments
Closed

mypy complains about monkey-patching classes in another module #1152

timabbott opened this issue Jan 25, 2016 · 7 comments
Labels
false-positive mypy gave an error on correct code feature priority-1-normal

Comments

@timabbott
Copy link

This issue affects unannotated code. Here's some example code from https://github.com/zulip/zulip/blob/master/zerver/lib/test_helpers.py#L47:

@contextmanager
def simulated_queue_client(client):
    real_SimpleQueueClient = queue_processors.SimpleQueueClient
    queue_processors.SimpleQueueClient = client
    yield
    queue_processors.SimpleQueueClient = real_SimpleQueueClient

(There's a similar issue with replacing a function rather than a class; and an example a few lines down in the Zulip codebase)

@JukkaL JukkaL added the feature label Jan 25, 2016
@JukkaL
Copy link
Collaborator

JukkaL commented Jan 25, 2016

Can you give an example where replacing a function fails? Some kinds of function monkey patching should work, and it would be nice to have an example that is still broken.

In general, type checking the compatibility of classes is hard, but at least these should be fixed to work:

mod.Cls = cast(Any, mod.Cls2)

def f():  # no annotation -> don't complain!
    mod.Cls = mod.Cls2

This is a little trickier:

def f() -> None:
    mod.Cls = mod.Cls2   # When exactly should this be okay? When should it not be okay?

@timabbott
Copy link
Author

Hmm I can't reproduce the function issue so let's remove that from this issue for now; I'll open another if I can reproduce.

@timabbott timabbott changed the title mypy complains about monkey-patching classes or functions in another module mypy complains about monkey-patching classes in another module Jan 26, 2016
@gvanrossum
Copy link
Member

General suggestion: can you copy/paste the exact error message(s) you got from mypy? Those are often invaluable hints for finding the root of the problem (and figuring out how to run mypy on exactly the code you were using takes much longer).

@timabbott
Copy link
Author

Oops, yeah, sorry about that, this is the error:

zerver/lib/test_helpers.py: note: In function "simulated_queue_client":
zerver/lib/test_helpers.py:57: error: Invalid assignment target
zerver/lib/test_helpers.py:59: error: Invalid assignment target

Acidity pushed a commit to Acidity/grouper that referenced this issue Jul 11, 2016
Mypy doesn't support this kind of conditional declaration based
on catching ImportError. See mypy issue #1152
(python/mypy#1152) for more information.
@gvanrossum gvanrossum added this to the 0.5 milestone Oct 17, 2016
@gvanrossum gvanrossum removed this from the 0.5 milestone Mar 29, 2017
@JukkaL
Copy link
Collaborator

JukkaL commented May 18, 2018

Here's a complete example which should probably not generate an error:

# main.py
from typing import Any
import m

def f(x: Any) -> None:
    m.C = x  # Cannot assign to a type
# m.py
class C: pass

@JukkaL JukkaL added the false-positive mypy gave an error on correct code label May 18, 2018
@blueyed
Copy link
Contributor

blueyed commented May 31, 2019

I've tried this method to change/enhance the types of attributes for a class from an external module this way.

# m.py
class C: pass
from typing import cast
import m

class TypedC:
    typed_attr: str

m.C = TypedC  # Cannot assign to a type
m.C = cast(TypedC, m.C)  # Cannot assign to a type

# works - a bit surprising (you might expect this to e.g. use `Any` then).
m.C = TypedC  # type: ignore

(Using setattr avoids the error, but does not use the new type then.)

@hauntsaninja
Copy link
Collaborator

I'm fine with mypy complaining about this. Monkeypatching classes is unsound given nominal typing.

@hauntsaninja hauntsaninja closed this as not planned Won't fix, can't repro, duplicate, stale Apr 14, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
false-positive mypy gave an error on correct code feature priority-1-normal
Projects
None yet
Development

No branches or pull requests

5 participants