News for hackers. Stuff that matters.
Re:Type hints cheat sheet (Python 3) by philwelch on Tuesday December 11, 2018 @10:41PM (#18661203)
I've used Python type hints for awhile now and have some mixed reviews.

First, the good: the extra safety and autocomplete functionality is very helpful once you have everything up and running. I feel a lot better about the type-safety of my code when I have a static typing mechanism; even if it's optional, I still get to use it. Also, it's very helpful that you can further specify optional types, container types, or Callable types.

Now, the bad:

* While you can specify a type as "Optional", there isn't (unless it's been recently added) a way for any of the tooling that consumes these type hints to actually enforce that types that you don't specify as Optional are non-Optional. Python makes it pretty hard to get None, compared to the relative ease of Ruby's nils and Java's null references, but I won't be fooled again.

* It feels weird to have to explicitly import things from `typing`. Is there an actual dummy class called `typing.Any` that will magically pass a type comparison against any other Python object? I dunno, it feels weird to import typing metadata the same way as actual code.

* If you use any third-party libraries, it is a serious pain to import the actual class that you want to use as a type annotation. It's either impossible or nigh-impossible with boto3 in particular.

Re:Type hints cheat sheet (Python 3) by ubernostrum on Tuesday December 11, 2018 @11:15PM (#18661349)
Is there an actual dummy class called `typing.Any` that will magically pass a type comparison against any other Python object?

There is an object called "typing.Any". It's a singleton, immutable, un-subclassable sentinel object for static type checking tools to recognize and handle. Runtime type checkers cannot work with it; it actually overrides both the __instancecheck__ and the __subclasscheck__ special methods to raise exceptions and forbid attempts to use runtime isinstance() or issubclass() checks against Any.