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

Circumventing Name "_" already defined mypy error #268

Closed
hukkin opened this issue Jun 10, 2021 · 5 comments
Closed

Circumventing Name "_" already defined mypy error #268

hukkin opened this issue Jun 10, 2021 · 5 comments

Comments

@hukkin
Copy link
Contributor

hukkin commented Jun 10, 2021

Mypy really hates the pattern of naming every test like def _(): and errors on every Ward test with the message Name "_" already defined on line 79 [no-redef].

Type checking tests still often makes sense, so we should come up with a good way to circumvent the error and document that. Best would perhaps be if mypy had a configuration option to disable this specific error only, and only disable it in a test directory. That doesn't seem to be possible though.

Adding a # type: ignore[no-redef] after each @test decorator line works but is a bit laborious. Maybe make an auto-formatter (supporting pre-commit framework) that adds these ignore comments? Or even better, maybe there's a way to make a mypy plugin that tricks mypy into thinking these comments are in place, even if they really aren't (less clutter in the codebase).

@darrenburns
Copy link
Owner

Ugh, that's unfortunate.

It's discussed in detail at python/mypy#2904 in the context of functools.singledispatch (which is actually what inspired Ward's use of _ - it acted as precedent :)) and it seems a mypy plugin is likely the way to go.

@hukkin
Copy link
Contributor Author

hukkin commented Jun 10, 2021

Cool, didn't think about searching mypy issue tracker but yeah that's precisely the same problem. So even more incentive and use cases for the mypy plugin it seems.

@hukkin
Copy link
Contributor Author

hukkin commented Aug 21, 2021

As a temporary solution I've found out that configuring

disable_error_code = ["no-redef"]

works well.

Unfortunately it seems that it can not be configured on a per module basis, so it will disable no-redef checks globally outside the test directory as well. One way to get around this limitation is using a separate command line invocation for tests and then using the CLI argument instead of configuration file:

mypy my-project/
mypy --disable-error-code=no-redef tests/

Alternatively, if using pre-commit to run mypy, it is possible to configure two separate mypy invocations in .pre-commit-config.yaml as well.

@cjolowicz
Copy link

cjolowicz commented Oct 10, 2021

This was recently fixed in mypy master:

A quick loosely related question (happy to move it to a separate issue):

Do you have plans for allowing strict type-checking of tests using the default argument syntax? The issue I see here is that the parameter should be annotated with the type of the value injected into the test, but the default argument has the type of the fixture that provides the value. It seems analogous to how dataclasses.field works, and would probably require a mypy plugin as well?

@hukkin
Copy link
Contributor Author

hukkin commented Dec 16, 2021

As @cjolowicz mentioned this was fixed in mypy and is now released in mypy 0.920 so there is no longer an issue. I'm closing the issue.

@cjolowicz I moved your question to #310 so it can be discussed in an open issue.

@hukkin hukkin closed this as completed Dec 16, 2021
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

3 participants