You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Dependent types have been discussed before (#366, #3062). I am currently dealing with an actual use case where something like the TypeMapping proposed here #3062 (comment) would be really useful.
That is, I am writing code for Time-Series Data Analysis that is relatively generic and could potentially support a variate of different datetime and timedelta formats out there, such as:
datetime.datetime / datetime.timedelta
numpy.datetime64 / numpy.timedelta64
pandas.Timestamp / pandas.Timedelta
int/int or float/float
Crucially, an issue that makes it complicated to provide generic type hints is that the timedelta-type should be compatible with the datetime type. If one of them comes from the pandas family, the other should as well.
Example function:
defgrid(
tmin: DT, tmax: DT, timedelta: TD, offset: Optional[DT] =None
) ->list[int]:
r"""Compute $\{k∈ℤ∣ tₘᵢₙ ≤ t₀+k⋅Δt ≤ tₘₐₓ\}$."""offset=tminifoffsetisNoneelseoffsetzero_dt=tmin-tmin# generates zero variable of correct typeasserttimedelta>zero_dt, "Assumption delta>0 violated!"asserttmin<=offset<=tmax, "Assumption: xmin≤xoffset≤xmax violated!"a=tmin-offsetb=tmax-offsetkmax=int(b//timedelta) # need int() in case both are floatskmin=int(a//timedelta)
returnlist(range(kmin, kmax+1))
This function does indeed work with all the types mentioned above. And in principle, we could solve the type-hinting problem here by doing a bunch of @overloads.
Though, the problem starts to become bigger when we start talking about writing lots of these generic functions or classes with multiple methods.
Proposal
Apart from the TypeMapping suggested here #3062 (comment), another possibility could be to introduce an additional syntax for @overload similar to how @pytest.mark.parametrize works, i.e.
Yeah, feature requests that affect the typing community as a whole don't belong in this repo. There are lots of other type checkers, so if you want to change the spec in some way, you should start a discussion at https://github.com/python/typing or the typing-sig mailing list
Dependent types have been discussed before (#366, #3062). I am currently dealing with an actual use case where something like the
TypeMapping
proposed here #3062 (comment) would be really useful.That is, I am writing code for Time-Series Data Analysis that is relatively generic and could potentially support a variate of different
datetime
andtimedelta
formats out there, such as:datetime.datetime
/datetime.timedelta
numpy.datetime64
/numpy.timedelta64
pandas.Timestamp
/pandas.Timedelta
int
/int
orfloat
/float
Crucially, an issue that makes it complicated to provide generic type hints is that the
timedelta
-type should be compatible with thedatetime
type. If one of them comes from the pandas family, the other should as well.Example function:
This function does indeed work with all the types mentioned above. And in principle, we could solve the type-hinting problem here by doing a bunch of
@overloads
.Though, the problem starts to become bigger when we start talking about writing lots of these generic functions or classes with multiple methods.
Proposal
Apart from the
TypeMapping
suggested here #3062 (comment), another possibility could be to introduce an additional syntax for@overload
similar to how@pytest.mark.parametrize
works, i.e.The text was updated successfully, but these errors were encountered: