13

In Python 3.9 we can use type hinting in a lowercase built-in fashion (without having to import type signatures from the typing module) as described here:

def greet_all(names: list[str]) -> None:
    for name in names:
        print("Hello", name)

I like very much this idea and I would like to know if it is possible to use this way of type hinting but in previous versions of python, such as Python 3.7, where we have write type hinting like this:

from typing import List

def greet_all(names: List[str]) -> None:
    for name in names:
        print("Hello", name)
Andre Nevares
  • 711
  • 6
  • 21
kbnt
  • 143
  • 1
  • 6
  • 2
    Try adding `from __future__ import annotations` at the top of the file. This should at least make Python ignore this syntax, I don't know if mypy checks for Python-3.9-ness before using this. – L3viathan Sep 17 '20 at 13:38
  • Even if you *can* do this, I would argue that you *shouldn't*. Argument types should be interfaces like Sequence and Iterable, not concrete collections like dicts and lists. Program to interfaces! – Jared Smith Sep 17 '20 at 13:44
  • @JaredSmith I'm pretty sure `typing.List` already forces the object to be a `list` (although I do agree with you in principle). – L3viathan Sep 17 '20 at 13:46
  • @L3viathan it does, but there's also typing.Iterable and typing.Sequence and typing.Mapping, which are more flexible while retaining the correctness guarantees. – Jared Smith Sep 17 '20 at 13:48

1 Answers1

14

Simply, import annotations from __future__ and you should be good to go.

from __future__ import annotations

import sys
!$sys.executable -V #this is valid in iPython/Jupyter Notebook


def greet_all(names: list[str]) -> None:
    for name in names:
        print("Hello", name)
        
        
greet_all(['Adam','Eve'])

Python 3.7.6
Hello Adam
Hello Eve
alec_djinn
  • 10,104
  • 8
  • 46
  • 71
  • 1
    Ah, beat me by a second. Unfortunately though, Pycharm still doesn't like it. It raises an warning even though it doesn't cause an error. – Carcigenicate Sep 17 '20 at 13:38
  • 3
    A pre-3.9 version of `mypy` won't know what `list[str]` means, though. – chepner Sep 17 '20 at 13:39
  • This is not exactly what OP is describing; this is a feature in Python 3.10 where annotations won't be evaluated and will simply be stored as strings. OP is referring to the built-in generics from Python 3.9. But still, I guess the effect is the same... – Anakhand Sep 17 '20 at 13:39
  • @Carcigenicate That's a PyCharm bug though, IMO. – L3viathan Sep 17 '20 at 13:40
  • I guess some third-party software has to be updated yet. On Jupyter/iPython it works fine. – alec_djinn Sep 17 '20 at 13:40
  • 2
    @Anakhand From the PEP: "For use cases restricted to type annotations, Python files with the "annotations" future-import (available since Python 3.7) **can parameterize standard collections**, including builtins. To reiterate, that depends on the external tools understanding that this is valid." – Carcigenicate Sep 17 '20 at 13:40