mypy Type-checker Metaclass Tests
The mypy type-checker recently merged a pull request for metaclass support in mypy. The goal here is to test how far that support extends in Python 2, given mypy didn't even parse Python 2 metaclass declarations previously.
py2_example1.pyis testing cases where the metaclass is defined before its instance, then passing in by name to the
py2_example2.pyconsists of my test cases from python/mypy#2413, modified for python 2
py2_example3.pyis testing metaclasses defined as a child class named
py3_example1.pyis the equivalent of
py2/example1.pybut with Python 3 metaclass declarations
py3_example2.pyconsists of my test cases from python/mypy#2413
six.pyis testing metclasses defined like in
py2/example1.py, but declared using
If you're not familliar with Github Gist, you can use the URL of this gist to clone it as a
./setup.sh blah will set up a venv virtualenv at path
.venv if no path is passed) and install mypy at the metaclass support commit.
You will have to
source into the virtualenv after that. (
./run_mypy.sh will typecheck the example files using the current Python environment (make sure you're in a venv with mypy and Python 3).
It's pretty clear that metaclass detection is still not happening in mypy for Python 2 codebases, but if a metaclass is detected (ie in Python 3) then everything seems to work as expected.
Errors to do with the
six.py test case are due to the fact that
six.with_metaclass needs to be special-cased, and it hasn't been special-cased yet.
All in all, this is still fantastic work, and I'm very grateful for it. Still, it means that I'll have to look into updating the metaclass detection logic to support Python 2.
Since the pull request, several issues on the mypy repository have been updated and/or opened, which will fix all the above issues I found (among others):
- mypy doesn't understand
- Mypy doesn't recognize metaclass implementing getitem (#1771)
- Add Python 2 support for metaclass (#2823)
- Metaclass is not properly inherited when using multiple inheritance with int (#2824)
- Update the behaviour of the
typefunction for metaclasses (#2826)