May-19-2021, 12:44 PM
Hi, I'm trying to make a class that inherits from another one. The problem I'm having is related to imports, I'll try to explain as best as I can.
food_bot
├── __init__.py
├── price
│ ├── __init__.py
│ ├── item_pricing.py
│ └── pricing.py
This is the folder structure of the project (not complete, only the price folder is relevant for my problem).
In the __init__.py inside price I imported
In conclusion I want to be able to import Pricing like this,
I might not be understanding all that well what TYPE_CHECKING is really useful for (same thing for __future__.annotations), I tried reading the documentation but it seems I got it wrong.
Can someone help me?
food_bot
├── __init__.py
├── price
│ ├── __init__.py
│ ├── item_pricing.py
│ └── pricing.py
This is the folder structure of the project (not complete, only the price folder is relevant for my problem).
In the __init__.py inside price I imported
from .item_pricing import ItemPricing from .pricing import Pricing __all__ = [ 'ItemPricing', 'Pricing' ]I need to import Pricing in the file item_pricing.py and I did that using the full path to the class Pricing:
from __future__ import annotations from math import floor from typing import TYPE_CHECKING from food_bot.menu import MenuItem from food_bot.exceptions import ParameterError from food_bot.price.pricing import Pricing class ItemPricing(Pricing): ...I did this because, when importing Pricing like this from food_bot.price import Pricing (and not like this from food_bot.price.pricing import Pricing) a circular import begins; I then tried to put it under
if TYPE_CHECKING:
, the circular import was resolved but, because I use pricing for inheritance I got this error: NameError: name 'Pricing' is not defined
even though it is defined. In conclusion I want to be able to import Pricing like this,
food_bot.price import Pricing
as it is the point of my __init__.py AND use Pricing as base class for ItemPricing.I might not be understanding all that well what TYPE_CHECKING is really useful for (same thing for __future__.annotations), I tried reading the documentation but it seems I got it wrong.
Can someone help me?