Python Forum

Full Version: "Class already defined" while using typings.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I'm building a little program but I'm trying to make it more easily expandable for the future (without the need of much refactoring). I'm experimenting with thing I have never used before like typings.
I have a small typings file that looks like this (called typings.py)
from typing import (
	Union,
	Any,
	Optional
)

from typing import ForwardRef as ref

CustomLevel = ref("levels.level.CustomLevel")
OfficialLevel = ref("levels.level.OfficialLevel")

__all__ = (
	"Union",
	"Any",
	"Optional",

	"CustomLevel",
	"OfficialLevel",
)
I also have a level.py file under ./levels/. In the file I have a base class but also a CustomLevel and OfficialLevel. At the top of level.py I am importing the CustomLevel typing. The issue is I am getting an error saying:
Error:
class already defined (line 5)
because the import of the CustomLevel type is clashing with definition of the CustomLevel class, like so:
from ..typings import (
	CustomLevel
)

class CustomLevel(): #<--- erros here since 'customlevel' is already defined
	def __init__(self) -> None:
		pass
	
	def somefunc(self) -> CustomLevel:
		pass
I am actually using an already existing api as a sort of "guide". What I mean is I am looking at the code of the api, because I think it's pretty good code, and using some of the features I think will be helpful in my program (like typings). In their api they do exactly what I am doing where they import a type from their typing file, and then create a class of the same name. In theirs they don't get an error though.

How can I fix this error?