Skip to content

[WIP] mypy updates - #111

Closed
manu-chroma wants to merge 55 commits into
common-workflow-language:modernizefrom
manu-chroma:mypy_updates
Closed

manu-chroma wants to merge 55 commits into
common-workflow-language:modernizefrom
manu-chroma:mypy_updates

Conversation

@manu-chroma

@manu-chroma manu-chroma commented May 28, 2017 •

Copy link
Copy Markdown
Member

Created this PR to discuss mypy related improvements in detail

Typesheds status

@manu-chroma

Copy link
Copy Markdown
Member Author

Changes made: 420ea14

~/github/schema_salad · (mypy_updates±) $ tox -e py35-mypy                                                                                                                                                                            17:20:26
py35-mypy recreate: /home/manu/github/schema_salad/.tox/py35-mypy
py35-mypy installdeps: mypy==0.511, typed-ast==1.0.3, -rrequirements.txt
py35-mypy installed: appdirs==1.4.3,avro-python3==1.8.2,CacheControl==0.11.7,certifi==2017.4.17,chardet==3.0.3,idna==2.5,isodate==0.5.4,lockfile==0.12.2,mistune==0.7.3,mypy==0.511,packaging==16.8,pkg-resources==0.0.0,pyparsing==2.2.0,rdflib==4.2.2,rdflib-jsonld==0.4.0,requests==2.16.5,ruamel.yaml==0.14.0,six==1.10.0,typed-ast==1.0.3,typing==3.5.3.0,urllib3==1.21.1
py35-mypy runtests: PYTHONHASHSEED='2432188320'
py35-mypy runtests: commands[0] | make mypy
rm -Rf typeshed/2.7/ruamel/yaml
ln -s /home/manu/github/schema_salad/.tox/py35-mypy/lib/python3.5/site-packages/ruamel/yaml \
	typeshed/2.7/ruamel/
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
	 --warn-redundant-casts --warn-unused-ignores \
	 schema_salad
typeshed/2.7/ruamel/yaml/compat.py:15: error: Cannot find module named 'ruamel.ordereddict'
typeshed/2.7/ruamel/yaml/compat.py:15: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
typeshed/2.7/ruamel/yaml/compat.py:15: error: Name 'ordereddict' already defined
typeshed/2.7/ruamel/yaml/reader.py:199: error: None not callable
typeshed/2.7/ruamel/yaml/reader.py:205: error: "UnicodeDecodeError" has no attribute "object"
typeshed/2.7/ruamel/yaml/reader.py:205: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:208: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:211: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:213: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:215: error: "UnicodeDecodeError" has no attribute "encoding"
typeshed/2.7/ruamel/yaml/reader.py:215: error: "UnicodeDecodeError" has no attribute "reason"
typeshed/2.7/ruamel/yaml/comments.py:281: error: "CommentedBase" has no attribute "_yaml_get_column"; maybe "_yaml_get_pre_comment"?
typeshed/2.7/ruamel/yaml/representer.py:522: error: Incompatible types in assignment (expression has type "Union[Any, Dict[Any, Any]]", target has type List[Any])
schema_salad/schema.py:434: error: Type argument 1 of "make_valid_avro" has incompatible value "Union[Any, Dict[Any, Any], unicode]"
schema_salad/ref_resolver.py:75: note: unused 'type: ignore' comment
schema_salad/ref_resolver.py:81: note: unused 'type: ignore' comment
schema_salad/ref_resolver.py:84: note: unused 'type: ignore' comment
schema_salad/ref_resolver.py:809: note: unused 'type: ignore' comment
schema_salad/ref_resolver.py:847: note: unused 'type: ignore' comment
schema_salad/ref_resolver.py:1003: note: unused 'type: ignore' comment
schema_salad/schema.py:181: note: unused 'type: ignore' comment
schema_salad/validate.py:216: note: unused 'type: ignore' comment
Makefile:173: recipe for target 'mypy' failed
make: *** [mypy] Error 1
ERROR: InvocationError: '/usr/bin/make mypy'
__________________________________________________________________________________________________________________ summary ___________________________________________________________________________________________________________________
ERROR:   py35-mypy: commands failed

@AleksandrSl ruamel.yaml project spits out some weird error messages. Type annotations might need some work. https://bitbucket.org/ruamel/yaml

Also, I wanted to know how can you help out in improving mypy support?
Thanks. 😄

@AleksandrSl

Copy link
Copy Markdown

@manu-chroma Maybe errors are due to the version of typeshed, it's 2.7 and tests were for 3.5
I have little experience with typeshed, since I used annotations inside code and PyCharm type checker, but i will try to add mistune and cachecontrol missing typesheds.
By the way, what is the purpose beside 2-3 python compatible code, instead of just migrating cwltool to python3?

@mr-c

mr-c commented May 29, 2017

Copy link
Copy Markdown
Member

@AleksandrSl cwltool is reused in other projects that haven't migrated from Py2 so we have to maintain compatibility. I just opened a pull request to add cwltool to http://www.python3statement.org/

@manu-chroma

Copy link
Copy Markdown
Member Author

Hey @AleksandrSl

Maybe errors are due to the version of typeshed, it's 2.7 and tests were for 3.5

we're running mypy with --py2 flag currently so it runs the mypy keeping in mind that it is a python2.7 codebase.

I have little experience with typeshed, since I used annotations inside code and PyCharm type checker, but i will try to add mistune and cachecontrol missing typesheds.

whatever possible. 😄 I'll keep this PR updated and soon begin with python3 mypy work.

@manu-chroma

Copy link
Copy Markdown
Member Author

@mr-c, Can you help me out with these errors? 😄 Almost all of them are related to ruamel package.

(venv) [venv] ~/github/schema_salad · (mypy_updates±) $ tox -e py35-mypy                                                                                                                                                              13:16:35
py35-mypy installed: appdirs==1.4.3,avro-python3==1.8.2,CacheControl==0.11.7,certifi==2017.4.17,chardet==3.0.3,idna==2.5,isodate==0.5.4,lockfile==0.12.2,mistune==0.7.3,mypy==0.511,packaging==16.8,pyparsing==2.2.0,rdflib==4.2.2,rdflib-jsonld==0.4.0,requests==2.17.3,ruamel.yaml==0.14.0,six==1.10.0,typed-ast==1.0.3,typing==3.5.3.0,urllib3==1.21.1
py35-mypy runtests: PYTHONHASHSEED='4041677850'
py35-mypy runtests: commands[0] | make mypy
rm -Rf typeshed/2.7/ruamel/yaml
ln -s /home/manu/github/schema_salad/.tox/py35-mypy/lib/python3.5/site-packages/ruamel/yaml \
	typeshed/2.7/ruamel/
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
	 --warn-redundant-casts --warn-unused-ignores \
	 schema_salad
typeshed/2.7/ruamel/yaml/compat.py:15: error: Cannot find module named 'ruamel.ordereddict'
typeshed/2.7/ruamel/yaml/compat.py:15: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
typeshed/2.7/ruamel/yaml/compat.py:15: error: Name 'ordereddict' already defined
typeshed/2.7/ruamel/yaml/reader.py:199: error: None not callable
typeshed/2.7/ruamel/yaml/reader.py:205: error: "UnicodeDecodeError" has no attribute "object"
typeshed/2.7/ruamel/yaml/reader.py:205: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:208: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:211: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:213: error: "UnicodeDecodeError" has no attribute "start"
typeshed/2.7/ruamel/yaml/reader.py:215: error: "UnicodeDecodeError" has no attribute "encoding"
typeshed/2.7/ruamel/yaml/reader.py:215: error: "UnicodeDecodeError" has no attribute "reason"
typeshed/2.7/ruamel/yaml/comments.py:281: error: "CommentedBase" has no attribute "_yaml_get_column"; maybe "_yaml_get_pre_comment"?
typeshed/2.7/ruamel/yaml/representer.py:522: error: Incompatible types in assignment (expression has type "Union[Any, Dict[Any, Any]]", target has type List[Any])
Makefile:169: recipe for target 'mypy' failed
make: *** [mypy] Error 1
ERROR: InvocationError: '/usr/bin/make mypy'
__________________________________________________________________________________________________________________ summary ___________________________________________________________________________________________________________________
ERROR:   py35-mypy: commands failed

@AleksandrSl

Copy link
Copy Markdown

@manu-chroma Error typeshed/2.7/ruamel/yaml/compat.py:15: error: Cannot find module named 'ruamel.ordereddict' is due to ruamel.ordereddict package is only works with python2 and is not installed with ruamel.yaml when py35-mypy is runned. It results in no real error though, since this case is handled in
typeshed/2.7/ruamel/yaml/compat.py and when python3 is runned standard collections.OrderedDict is used. So you just need to add suggested --ignore-missing-imports, or rewrite ruaml.yaml package)

@mr-c

mr-c commented May 31, 2017

Copy link
Copy Markdown
Member

Thanks @AleksandrSl , you are correct. Ideally ruamel.yaml would update their in-line annotations.

@manu-chroma Is there a way to have mypy skip issues with the ruamel.* packages for now?

@AleksandrSl

Copy link
Copy Markdown

@mr-c Commenting directory removal and symlink creation in Makefile will help with ruamel

#       rm -Rf typeshed/2.7/ruamel/yaml
#       ln -s $(shell python -c 'from __future__ import print_function; import ruamel.yaml; import os.path; print(os.path.dirname(ruamel.yaml.__file__))') \
#               typeshed/2.7/ruamel/

@manu-chroma

Copy link
Copy Markdown
Member Author

So these errors didn't come up because mypy==0.470 just didn't catch them or wasn't strict enough?
That's what I find confusing right now, that why didn't these errors come up before.

@mr-c

mr-c commented May 31, 2017

Copy link
Copy Markdown
Member

@manu-chroma mypy has been changing a fair amount between releases, that's why

@mr-c

mr-c commented May 31, 2017

Copy link
Copy Markdown
Member

@AleksandrSl I am surprised to hear that -- since ruamel.yaml ships their own type hints within their code those lines make sure they are available to mypy. Did the recent release of mypy finally improve consumption of external type information?

@AleksandrSl

Copy link
Copy Markdown

@mr-c I think that my solution is just a trick, not a real one. If these lines are commented it seems that ruamel package is not checked at all, but i don't see better way to get rid of these errors. Maybe additional typeshed for ruamel that will correct inconsistencies inside ruamel annotations can help.

@manu-chroma

manu-chroma commented May 31, 2017 •

Copy link
Copy Markdown
Member Author

I would like to point out that I'm using ruamel==0.14.0 which contains updates for mypy --strict. It was released in March end after Micheal created issue on their repo regarding the same issue.

Did the recent release of mypy finally improve consumption of external type information?

I also think this might be the case.

@manu-chroma

manu-chroma commented May 31, 2017 •

Copy link
Copy Markdown
Member Author

Tried using --ignore-missing-imports and commented out those two lines from Makefile. Here's the output,

venv) [venv] ~/github/schema_salad · (mypy_updates±) $ tox -e py35-mypy                                                                                                                                                              19:20:05
py35-mypy installed: appdirs==1.4.3,avro-python3==1.8.2,CacheControl==0.11.7,certifi==2017.4.17,chardet==3.0.3,idna==2.5,isodate==0.5.4,lockfile==0.12.2,mistune==0.7.3,mypy==0.511,packaging==16.8,pyparsing==2.2.0,rdflib==4.2.2,rdflib-jsonld==0.4.0,requests==2.17.3,ruamel.yaml==0.14.0,six==1.10.0,typed-ast==1.0.3,typing==3.5.3.0,urllib3==1.21.1
py35-mypy runtests: PYTHONHASHSEED='1735989360'
py35-mypy runtests: commands[0] | make mypy
rm -Rf typeshed/2.7/ruamel/yaml
# ln -s /home/manu/github/schema_salad/.tox/py35-mypy/lib/python3.5/site-packages/ruamel/yaml \
	# typeshed/2.7/ruamel/
MYPYPATH=typeshed/2.7 mypy --py2 --disallow-untyped-calls \
	 --warn-redundant-casts --warn-unused-ignores \
	 --warn-unused-ignores \
	 schema_salad
schema_salad/tests/test_examples.py:10: error: Cannot find module named 'ruamel.yaml'
schema_salad/tests/test_examples.py:10: note: (Perhaps setting MYPYPATH or using the "--ignore-missing-imports" flag would help)
schema_salad/tests/test_examples.py:20: error: Cannot find module named 'ruamel.yaml.comments'
schema_salad/tests/test_fetch.py:9: error: Cannot find module named 'ruamel.yaml'
schema_salad/jsonld_context.py:9: error: Cannot find module named 'ruamel.yaml'
schema_salad/ref_resolver.py:26: error: Cannot find module named 'ruamel.yaml'
schema_salad/ref_resolver.py:27: error: Cannot find module named 'ruamel.yaml.comments'
schema_salad/schema.py:8: error: Cannot find module named 'ruamel.yaml'
schema_salad/schema.py:31: error: Cannot find module named 'ruamel.yaml.comments'
schema_salad/sourceline.py:2: error: Cannot find module named 'ruamel.yaml'
schema_salad/sourceline.py:3: error: Cannot find module named 'ruamel.yaml.comments'
schema_salad/sourceline.py:13: error: Name 'ruamel.yaml.comments.CommentedBase' is not defined
schema_salad/sourceline.py:14: error: Module has no attribute "yaml"
schema_salad/sourceline.py:29: error: Name 'ruamel.yaml.comments.CommentedBase' is not defined
schema_salad/sourceline.py:91: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:92: error: Argument 1 to "iteritems" has incompatible type "Union[int, float, str, unicode, Dict[Any, Any], List[Any]]"; expected Mapping[Any, Any]
schema_salad/sourceline.py:93: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:94: error: Unsupported target for indexed assignment
schema_salad/sourceline.py:94: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:96: error: Unsupported target for indexed assignment
schema_salad/sourceline.py:99: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:100: error: Argument 1 to "enumerate" has incompatible type "Union[int, float, str, unicode, Dict[Any, Any], List[Any]]"; expected Iterable[Any]
schema_salad/sourceline.py:101: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:102: error: Unsupported target for indexed assignment
schema_salad/sourceline.py:102: error: Some element of union has no attribute "lc"
schema_salad/sourceline.py:104: error: Unsupported target for indexed assignment
schema_salad/sourceline.py:165: error: Module has no attribute "yaml"
schema_salad/ref_resolver.py:524: error: No overload variant of "__getitem__" of "unicode" matches argument types [builtins.unicode]
schema_salad/ref_resolver.py:1005: error: Dict[unicode, Union[Dict[Any, Any], unicode, Iterable[unicode]]] has no attribute "lc"
schema_salad/ref_resolver.py:1006: error: Dict[unicode, Union[Dict[Any, Any], unicode, Iterable[unicode]]] has no attribute "lc"
schema_salad/schema.py:240: error: No overload variant of "__getitem__" of "unicode" matches argument types [builtins.str]
schema_salad/schema.py:242: error: No overload variant of "__getitem__" of "unicode" matches argument types [builtins.str]
schema_salad/schema.py:289: error: List[Dict[unicode, Any]] has no attribute "lc"
schema_salad/schema.py:289: error: Some element of union has no attribute "lc"
schema_salad/schema.py:290: error: List[Dict[unicode, Any]] has no attribute "lc"
schema_salad/schema.py:290: error: Some element of union has no attribute "lc"
schema_salad/tests/test_examples.py:18: note: unused 'type: ignore' comment
schema_salad/jsonld_context.py:13: note: unused 'type: ignore' comment
Makefile:169: recipe for target 'mypy' failed
make: *** [mypy] Error 1
ERROR: InvocationError: '/usr/bin/make mypy'

We're using classes from ruamel at a few places to indicate types in the codebase. Those places are showing errors now. Also, when adding attributes to objects created from classes in ruamel.

@mr-c

mr-c commented May 31, 2017

Copy link
Copy Markdown
Member

Note: I do nod recommend commenting out the ruamel.yaml lines in the Makefile :-)

@manu-chroma

manu-chroma commented May 31, 2017 •

Copy link
Copy Markdown
Member Author

@mr-c Just trying out the possibilities. 😄 So, when using --ignore-missing-imports and doing no other changes to Makefile shows the same errors as #111 (comment)

I'll try to find other ways to selectively suppress warnings from ruamel package.

@AleksandrSl

AleksandrSl commented Jun 4, 2017 •

Copy link
Copy Markdown

@manu-chroma I have added typeshed for mistune -
https://github.com/AleksandrSl/schema_salad/blob/python3-support/typeshed/2and3/mistune.pyi
I think it should work both for python 2 and 3, and current typeshed can be replaced with it, since most types in current version are annotated as any. I will try to make pull-request to mistune-contrib repo to check my typeshed more thoroughly

@manu-chroma

Copy link
Copy Markdown
Member Author

@AleksandrSl this looks great! I will try it out. 😃

If you're able to get permission from the mistune people, you can submit this typeshed in https://github.com/python/typeshed/

@manu-chroma manu-chroma changed the title [WIP]: mypy updates [WIP] mypy updates Jun 5, 2017
@manu-chroma

manu-chroma commented Jun 7, 2017 •

Copy link
Copy Markdown
Member Author

@AleksandrSl I've replaced Union[str, Text] with Text and added your stub file for mistune package. 😄

@AleksandrSl

Copy link
Copy Markdown

@manu-chroma

Copy link
Copy Markdown
Member Author

mypy passing on Travis now. 😌
https://travis-ci.org/common-workflow-language/schema_salad/jobs/240265574#L268

Next step would be to run without the --py2 flag.

@manu-chroma

Copy link
Copy Markdown
Member Author

I have raised an issue about ruamel errors

@AleksandrSl This would definitely help. In the meantime, I've ignored ruamel errors by adding it in mypy.ini file, so this doesn't disrupt our development. I will refer to your typeshed status for py3 now.

@AleksandrSl

Copy link
Copy Markdown

@manu-chroma @mr-c Some errors were fixed in ruamel: fix commit, issue
but not all, and the nature of the fixes and overall annotations in ruamel seems strange to me(e.g # type: (Any, Optional[Any], Optional[Any]) -> None).

@manu-chroma

Copy link
Copy Markdown
Member Author

I've gone through the fix commit. It's mostly using Any type for the purpose of silencing the warnings. Works for our case, so I'd say it's good enough.

Main changes:
- use of __future__
- using six lib to:
	- deal with checking unicode
	- dict operations
	- other API changes
- use update ruamel.yaml package which includes more mypy annotations
author = "Aleksandr Slepchenkov"
email = "[email protected]"
…in py3 mode

and move to typshed/2and3/ directory
@manu-chroma

Copy link
Copy Markdown
Member Author

changes from here are now incorporated in #109

The purpose of this branch was to separately track mypy progress. Closing it now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants