question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Circumventing `Name "_" already defined` mypy error

See original GitHub issue

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).

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:5 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
hukkincommented, 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.

1reaction
cjolowiczcommented, 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?

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to circumvent 'name already defined error' in mypy?
When I run mypy against this, it will have the following error: Name 'A' already defined (possibly by an import).
Read more >
Error codes enabled by default - mypy 0.991 documentation
Check that each name is defined once [no-redef]#. Mypy may generate an error if you have multiple definitions for a name in the...
Read more >
Mypy Documentation - Read the Docs
Mypy is a static type checker for Python. Type checkers help ensure that you're using variables and functions in your code correctly.
Read more >
More Precise Types - Real Python
mypy float_check.py float_check.py:8: error: Argument 1 to "double" has ... After creating the Named protocol, and defining the function greet() that takes ...
Read more >
django-stubs - PyPI
Mypy stubs for Django. ... will cause this error message: ... WithAnnotations[Model] , which indicates that the Model has been annotated, meaning it...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found