Python Forum
"Class already defined" while using typings.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
"Class already defined" while using typings.
#1
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?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python library not defined in user defined function johnEmScott 2 508 May-30-2020, 04:14 AM
Last Post: DT2000
  Python complains that class instance is not defined colt 3 653 Sep-17-2019, 12:32 AM
Last Post: ichabod801
  saving (in text or binary) an object under a defined class cai0824 3 883 May-12-2019, 08:55 AM
Last Post: snippsat
  "not defined" error in function referencing a class Exsul 2 1,247 Mar-27-2019, 11:59 PM
Last Post: Exsul
  Creating class - name not defined error python_alex 3 4,387 Jun-30-2018, 11:17 PM
Last Post: snippsat
  name a thread defined from a class ricardons 1 1,003 Mar-10-2018, 10:59 PM
Last Post: ricardons

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020