-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add special handling for typing.get_args #17784
base: master
Are you sure you want to change the base?
Conversation
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
Diff from mypy_primer, showing the effect of this PR on open source code: scipy (https://github.com/scipy/scipy)
+ scipy/signal/tests/test_short_time_fft.py:419: error: Argument "fft_mode" to "ShortTimeFFT" has incompatible type "str | int | None"; expected "Literal['twosided', 'centered', 'onesided', 'onesided2X']" [arg-type]
+ scipy/signal/tests/test_short_time_fft.py:419: error: Argument "mfft" to "ShortTimeFFT" has incompatible type "str | int | None"; expected "int | None" [arg-type]
+ scipy/signal/tests/test_short_time_fft.py:420: error: Argument "scale_to" to "ShortTimeFFT" has incompatible type "str | int | None"; expected "Literal['magnitude', 'psd'] | None" [arg-type]
+ scipy/signal/tests/test_short_time_fft.py:420: error: Argument "phase_shift" to "ShortTimeFFT" has incompatible type "str | int | None"; expected "int | None" [arg-type]
|
With respect to the primer results those errors seem fine. pp = dict(
fft_mode=get_args(FFT_MODE_TYPE),
mfft=[None, n, n+1, n+2],
scaling=[None, 'magnitude', 'psd'],
phase_shift=[None, -n+1, 0, n // 2, n-1])
for f_typ, mfft, scaling, phase_shift in product(*pp.values()):
if f_typ == 'onesided2X' and scaling is None:
continue # this combination is forbidden
SFT = ShortTimeFFT(w, h_n, fs=n, fft_mode=f_typ, mfft=mfft,
scale_to=scaling, phase_shift=phase_shift)
#<snip from ShortTimeFFT definition>
_fft_mode: FFT_MODE_TYPE = 'onesided' # Mode of FFT to use It's complaining As for the other three related additions - they were only absent prior because before this Worth someone else taking a look at this now. |
This PR adds special handling for
typing.get_args
when the input is aUnion[Literal...]
allowing it to return that type instead of the current behavior of returningAny
. Adds the handling and related test cases forget_args
with varying types.This allows mypy to understand when it is used as a type guard.
Fixes #15106