I am working with time series, and want to retrieve and slice by date/time. Ideally, I'd leverage the existing [:] syntax.
For example (see last line):
from datetime import datetime
from typing import Union
class T:
def __getitem__(self, key: datetime | slice) -> Union['T', float]:
if isinstance(key, slice):
return T()
if isinstance(key, datetime):
return 42
raise ValueError()
t = T()
v = t[datetime(2020, 1, 1)] # OK
t2 = t[datetime(2020, 1, 1):datetime(2021, 1, 1)] # Not OK
This runs fine, but when running it through mypy, it produces this error on the last line:
error: Slice index must be an integer, SupportsIndex or None [misc]
Found 1 error in 1 file (checked 1 source file)
Short of disabling this error altogether, is there a method to make this code pass mypy?
Or am I doing something I should not be doing?