fooiey
fooiey

Reputation: 1590

How to circumvent 'name already defined error' in mypy?

So I'm doing type annotations on some code. And this code follows the following convention.

I have some kind of base module that defines certain classes

#module1.py
class A():
  attr1 = 'hello'
  attr2 = 'world'

class B():
  attr3 = 'hi'
  attr4 = 'earth'

And then in some other modules, I import and inherit from them and override some of the attributes.

#module2.py
from module1 import *
class A(A):
  attr1 = 'bye'

class B(B):
  attr3 = 'goodbye'

When I run mypy against this, it will have the following error: Name 'A' already defined (possibly by an import)

Is there any way I can go about fixing this without adding # type: ignore to each instance of this error that comes up in mypy? The only other solution I could think of is to rename the base class, e.g. class ABase():, but that is not possible. Because in some modules, A will not be overwritten but I still need access to A from that module. E.g. module3 might be

from module1 import *
class B(B):
  attr3 = 'something'

So here, I don't override A, but I still need to be able to access module3.A.attr1, which is not possible if I rename the base class. Is this possible, or should I just resort to ignoring all the errors. And if so, is there a single configuration setting I can add to my mypy.ini? I couldn't find any here.

Upvotes: 4

Views: 5963

Answers (3)

koks der drache
koks der drache

Reputation: 1849

In my case the error was caused by a Pydantic Class where two fields had the same alias.

Upvotes: 1

l0b0
l0b0

Reputation: 58958

@YevhenKuzmovych's solution is the quickest one; another alternative would be to rename either the parent or child class. Having two different classes of the same name in the same context can't be good, and is likely to cause hard-to-debug issues down the line:

  • When you see an A in isolation you don't know which A it is.
  • The two As must have different semantics, or they would be a single class. So there is no way to tell from the code whether one class or the other was intended with a quick inspection.

Ignore this warning at your peril.

Upvotes: 2

Yevhen Kuzmovych
Yevhen Kuzmovych

Reputation: 12140

Try defining an alias for the base classes:

from module1 import A as ABase

class A(ABase):
    ...

But you should probably just change the names of the classes if you can.

Upvotes: 3

Related Questions