"Class already defined" while using typings. - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: "Class already defined" while using typings. (/thread-29127.html) |
"Class already defined" while using typings. - DreamingInsanity - Aug-19-2020 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: 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: passI 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? |