On Wed, Jul 4, 2018, 9:36 AM Steven D'Aprano < steve+comp.lang.pyt...@pearwood.info> wrote:
> On Wed, 04 Jul 2018 13:48:26 +0100, Bart wrote: > >> A better example would be: > >> > >> x: int = None > >> > >> which ought to be read as "x is an int, or None, and it's currently > >> None". > > > > In that case the type hint is lying. > > "Practicality beats purity." > > "This type, or None" is such a common pattern that any half-way decent > type checker ought to be able to recognise it. You can, of course, > explicitly annotate it: > > x: Optional[int] = None > > but the type checker should infer that if you assign None to a variable > which is declared int, you must have meant Optional[int] rather than just > int. If it doesn't, get a better type checker. > > Note that None is a special case (because sometimes special cases *are* > special enough to break the rules). I don't think this case is special enough. As a person coming along later and trying to read the code, I should be able to trust that the type hint means what it says. I should not have to go look up the rules of the linter to infer that in this case, type A actually means type B. Optimize for readability, not writability. -- https://mail.python.org/mailman/listinfo/python-list