News for hackers. Stuff that matters.
Re:Python Type Hints – How to Handle Optional Imports by bxparks on Wednesday January 05, 2022 @12:29AM (#29805500)
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.

Re:Python Type Hints – How to Handle Optional Imports by pydry on Wednesday January 05, 2022 @05:49AM (#29807479)
Pokemon 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.

Re:Python Type Hints – How to Handle Optional Imports by _aavaa_ on Wednesday January 05, 2022 @10:37AM (#29810261)
It 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).)

Re:Python Type Hints – How to Handle Optional Imports by nerdponx on Wednesday January 05, 2022 @02:36AM (#29806215)
Mypy has hard-coded support for `sys.version_info`.
Re:Python Type Hints – How to Handle Optional Imports by lozenge on Wednesday January 05, 2022 @07:00AM (#29807934)
And a command line argument to set its value.

If you're targeting multiple Python versions your CI should be running mypy multiple times.

Re:Python Type Hints – How to Handle Optional Imports by chromatin on Wednesday January 05, 2022 @08:11AM (#29808431)
> 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.

Re:Python Type Hints – How to Handle Optional Imports by adamchainz on Wednesday January 05, 2022 @01:20PM (#29812817)
Ah yes, nice hint that backports can use sys.version_info.