Repository: python/mypy

Issue:
Make Python 2 support an "extra" in 0.900
The motivation for this is that typed-ast is a pain to deal with (it's just broken again on latest Python 3.10, and was a pain to deal with for Python 3.9, e.g. preventing Windows installs for some months) and Python 2 usage is ever dwindling*. Since mypy 0.900 will basically force all users to revisit how they install mypy, it seems like a good time to force the remaining users of mypy with Python 2 to change to `pip install 'mypy[python2]'`. 

@JukkaL thoughts?

*pypistats.org tells me Python 2 usage is down 40% in the last six months (based on Python 2 downloads of six)


Discussion:
strongly agreed
The current setup.py says that 3.5+ is required, and I don't see wheels for Python 2 anymore. Is Python 2 still supported? It says so in quite a few places, including the GitHub summary.
@henryiii Python 2 was never supported for *running* mypy, but mypy still supports *checking* Python 2 code. Shantanu's suggestion is to make the support for checking Python 2 an extra.
I just realized that 🤦  Thanks!
Though I'm confused about the comments covering typed ast. I still see type comments used once in a while in Python 3 only code?
The ast  module in Python 3.8 added support for type comments. So we only need typed-ast when running on Python 3.7 and earlier, or when type checking Python 2 code (and running on any Python version).

Python 2 is still used a lot, so we can't stop supporting it in the near future. For example, about 10% of downloads for requests are for Python 2. I guess the main benefit of making Python 2 support optional would be that a broken typed-ast doesn't prevent installation of mypy on Python 3.8+, including alpha and beta releases? And typed-ast is unlikely to get broken on 3.7 and earlier, since these receive security fixes only.

I think that the proposal sounds reasonable, as long as we generate a helpful error message when running in Python 2 mode and typed-ast is not available. We should still make sure that typed-ast works on Python 3.10 at least, but it should be okay if it only gets fixed some time after the release of Python 3.10.0. It's too early to say if we will need Python 2 support when running on Python 3.11 (or whatever the next release after 3.10 will be called).
Then why not:

```python
install_requires=["typed_ast >= 1.4.0, < 1.5.0; python_version < '3.8'",
```

I don't get why typed comments are "Python 2"; they are perfectly valid in Python 3 only code; in fact, they were the only way to do a variable annotation before 3.6, and they were faster than instantiating an object before Python 3.10 (or for a codebase requiring 3.7+ by using the future import).
@henryiii The ast module doesn't allow you to parse Python 2 code, so on all versions of Python, mypy uses typed-ast to parse Python 2 code. On Python 3.8+, this is the only thing we need typed-ast for, hence this issue. 
Thanks for clearing up my confusion! :)

Resolving patch:
diff --git a/mypy/fastparse2.py b/mypy/fastparse2.py
--- a/mypy/fastparse2.py
+++ b/mypy/fastparse2.py
@@ -68,7 +68,8 @@
         from typed_ast import ast35  # type: ignore[attr-defined]  # noqa: F401
     except ImportError:
         print('The typed_ast package is not installed.\n'
-              'You can install it with `python3 -m pip install typed-ast`.',
+              'For Python 2 support, install mypy using `python3 -m pip install "mypy[python2]"`'
+              'Alternatively, you can install typed_ast with `python3 -m pip install typed-ast`.',
               file=sys.stderr)
     else:
         print('You need a more recent version of the typed_ast package.\n'
diff --git a/setup.py b/setup.py
--- a/setup.py
+++ b/setup.py
@@ -191,7 +191,7 @@ def run(self):
       classifiers=classifiers,
       cmdclass=cmdclass,
       # When changing this, also update mypy-requirements.txt.
-      install_requires=['typed_ast >= 1.4.0, < 1.5.0',
+      install_requires=["typed_ast >= 1.4.0, < 1.5.0; python_version<'3.8'",
                         'typing_extensions>=3.7.4',
                         'mypy_extensions >= 0.4.3, < 0.5.0',
                         'types-typing-extensions>=3.7.0',
@@ -199,7 +199,7 @@ def run(self):
                         'toml',
                         ],
       # Same here.
-      extras_require={'dmypy': 'psutil >= 4.0'},
+      extras_require={'dmypy': 'psutil >= 4.0', 'python2': 'typed_ast >= 1.4.0, < 1.5.0'},
       python_requires=">=3.5",
       include_package_data=True,
       project_urls={


Test patch: