4 ms·
I had to do a similar hack recently. The 'zoneinfo' library is available only on Python >=3.9. But the 'backports.zoneinfo' library is available for older Pytho
by bxparks 5y ago
I had to do a similar hack recently. The 'zoneinfo' library is available only on Python >=3.9. But the 'backports.zoneinfo' library is available for older Pythons. The recommended way of handling this causes mypy to spit out errors:
try:
import zoneinfo
except:
from backports import zoneinfo
The solution that I found to make mypy happy is:
import sys
if sys.version_info >= (3, 9):
import zoneinfo
else:
from backports import zoneinfo
I have no idea why mypy is happy.
If I can get type hinting to work, it does detect certain errors in certain situations. But the whole thing takes too much work. The syntax is awkward and klunky. I'm not always sure that mypy actually catches typing errors, even with the --strict flag. And Python package management is already a shit show, but when I add mypy to the mix, it spews incomprehensible error messages that I have no idea how to fix.
I'm starting to look at Golang to replace my Python usage. If I'm going to do the work of adding typing info, I'd rather do it in a language that has static type checking from the very beginning. And being able to compile down to a single binary is appealing.
- nerdponx 5y agoMypy has hard-coded support for `sys.version_info`.
- lozenge 5y agoAnd a command line argument to set its value. If you're targeting multiple Python versions your CI should be running mypy multiple times.
- pydry 5y agoPokemon exception handling is a bad idea in almost all instances. The first import can fail for any number of reasons apart from the expected one and that reason will be swallowed and potentially replaced by a different, misleading, second order error message or worse - strange hard to explain behavior. The second will behave (and more importantly, fail) in a much more predictable way.
- _aavaa_ 5y agoIt took me a few seconds to figure out what you were talking about with Pokemon. That's the first time I've heard that term! I love it. (For anyone else scratching their head, it's Pokemon exception handling cause the except block catches any and all exceptions (gotta catch 'em all).)
- chromatin 5y ago> I'm starting to look at Golang to replace my Python usage. I likewise switched to D and Rust and (excepting the difference in ecosystems richness compared to Python) am easily equally (or more) productive in statically typed languages.
- adamchainz 5y agoAh yes, nice hint that backports can use sys.version_info.