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 warning: tests/__init__.py:34: error: Cannot assign to a method #494

Closed
hjmjohnson opened this issue Jun 5, 2020 · 3 comments
Closed

Comments

@hjmjohnson
Copy link
Contributor

Describe the bug
mypy static checker provides warning:

tests/__init__.py:34: error: Cannot assign to a method

To Reproduce
Steps to reproduce the behavior:

  1. Modify setup.cfg and enable error checking for tests/__init__.py
  2. MYPYPATH=$(pwd)/monai mypy $(pwd)

Expected behavior
No warnings produced.

hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 5, 2020
hjmjohnson added a commit to BRAINSia/MONAI that referenced this issue Jun 6, 2020
wyli pushed a commit that referenced this issue Jun 6, 2020
* Add subcommands to the individual codeformat test

One often wants to run the very fast codeformatting tests without
running any of the unit tests. Add a commands to allow this
behavior with "./runtests.sh [--black|--pytype|--mypy|--flake8]".

One often wants the `black` formatting tool to automatically
fix formatting violations without requiring explicit
manual call. Use "./runtests.sh --black-fix"

Add mypy static checking tool to monai

Run the complete mypy static checking tool during evaluations
of code format style checks.

Related to #476 subtask for enable mypy

Expand the documentation for runtest.sh script.  Provide more expressive
documentation for `-h`

The dryrun request to print the commands used for static
and format checking without actually running the commands.

* Temporarily add branch needed to trigger CI of this codebase.

* Suppress mypy warnings about incompatible superclass signature

Related to #495

* Suppress mypy error: Cannot assign to a method

Related to #494

* Mypy potential type mismatch resolved with assertion.

* Mypy type ambiguity resolved.
@hjmjohnson
Copy link
Contributor Author

@benjamin-gorman Could you try to address this warning?

@ghost
Copy link

ghost commented Aug 4, 2020

Work arounds

# preferable (see context)

setattr(unittest.case._AssertWarnsContext, "__enter__", _enter_pr_4800)

# but has flake8-bugbear error
# B010 Do not call setattr with a constant attribute value, it is not any safer than normal property access.
# ignore the specific error

unittest.case._AssertWarnsContext.__enter__ = _enter_pr_4800  # type: ignore[assignment]

Context

I prefer setattr over using # type: ignore. Mypy won't complain about it. This is something we could discuss in the common issues section in the docs.

python/mypy#2427 (comment)

The has been no progress recently. The workarounds discussed above (setattr or # type: ignore) are still the recommended ways to deal with this.

python/mypy#2427 (comment)

Progression of _enter_pr_4800

Alternatively

There is the typing.Protocol fix but that would require a .pyi file and seems like overkill.

@wyli
Copy link
Contributor

wyli commented Nov 4, 2020

the workaround is well documented

MONAI/tests/__init__.py

Lines 32 to 36 in bbc2da5

# workaround for https://bugs.python.org/issue29620
try:
# Suppression for issue #494: tests/__init__.py:34: error: Cannot assign to a method
unittest.case._AssertWarnsContext.__enter__ = _enter_pr_4800 # type: ignore
except AttributeError:

closing this issue

@wyli wyli closed this as completed Nov 4, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants