I have two classes with annotations / type hints.
First one works without any problem:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bunyamin.models.exchange import Exchange
class Kline:
def read_klines(exchange: Exchange):
pass
Second one is really similar:
from typing import TYPE_CHECKING
if TYPE_CHECKING:
from bunyamin.models.timeframe import Timeframe
def normalize_dt(dt: datetime, timeframe: Timeframe) -> datetime: # -> This line raises NameError
pass
but raises NameError: name 'Timeframe' is not defined.
I know I can just use a string (like 'Timeframe') instead of the class itself, but AFAIK this is not the expected behavior. What am I missing?
Python version I'm using is 3.8.2, if that's relevant.
EDIT:
While I was trying to isolate the problem, I've omitted all of the "seemingly irrelevant" imports. But the first file actually contains from __future__ import annotations at the top, which makes it work. See the first answer for details.