Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include additional files to be typed checks, up to exceptions.py #1931

Closed
wants to merge 3 commits into from

Conversation

savarin
Copy link
Contributor

@savarin savarin commented Aug 23, 2020

Addresses #1897, as per discussion in #1897 (comment).

This PR includes additional files to be typed checked, up to exceptions.py. Note that both the .py and .pyi files are included in the checks.

In particular, the .pyi file from typeshed for exceptions.py are ported over and expanded to satisfy type checks.


class PoolError(HTTPError):
pool: Any
def __init__(self, pool: Any, message: str) -> None: ...
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The pool here is from connectionpool.py, but importing here would create a circular dependency.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're hitting circular imports you can sometimes use this construction:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from urllib3.connectionpool import ConnectionPool

class PoolError:
    pool: "ConnectionPool"

Comment on lines 23 to 29
class MaxRetryError(RequestError):
reason: Any
def __init__(self, pool: Any, url: str, reason=Optional[str]) -> None: ... # type: ignore

class HostChangedError(RequestError):
retries: Any
def __init__(self, pool: Any, url: str, retries=int) -> None: ... # type: ignore
Copy link
Contributor Author

@savarin savarin Aug 23, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm a little puzzled why I keep getting a mypy error here despite adding explicit types, so included an ignore.

src/urllib3/exceptions.pyi:25: error: Function is missing a type annotation for one or more arguments
src/urllib3/exceptions.pyi:29: error: Function is missing a type annotation for one or more arguments.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be retries: int instead of =int :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LOL I guess this is where I should have taken a break :D

@codecov
Copy link

codecov bot commented Aug 23, 2020

Codecov Report

Merging #1931 into master will increase coverage by 0.04%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1931      +/-   ##
==========================================
+ Coverage   99.77%   99.81%   +0.04%     
==========================================
  Files          24       24              
  Lines        2179     2179              
==========================================
+ Hits         2174     2175       +1     
+ Misses          5        4       -1     
Flag Coverage Δ
#unittests 99.35% <ø> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
src/urllib3/util/ssl_.py 100.00% <0.00%> (+1.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update df1f648...10cb500. Read the comment docs.

Copy link
Member

@sethmlarson sethmlarson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this, here are some comments for you :)

I'm wondering about the empty .pyi files, should we follow the dependency tree to commit type stubs for all the dependencies of a node before that node itself to not run into issues?

noxfile.py Show resolved Hide resolved

class PoolError(HTTPError):
pool: Any
def __init__(self, pool: Any, message: str) -> None: ...
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you're hitting circular imports you can sometimes use this construction:

from typing import TYPE_CHECKING

if TYPE_CHECKING:
    from urllib3.connectionpool import ConnectionPool

class PoolError:
    pool: "ConnectionPool"


class MaxRetryError(RequestError):
reason: Any
def __init__(self, pool: Any, url: str, reason=Optional[str]) -> None: ... # type: ignore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be :Optional[...] instead of =Optional[...], remove the # type: ignore after verifying this works

class LocationValueError(ValueError, HTTPError): ...

class LocationParseError(LocationValueError):
location: Any
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we know location is a str from the constructor? Can change from Any to str

@@ -1,5 +1,5 @@
from __future__ import absolute_import
from .packages.six.moves.http_client import IncompleteRead as httplib_IncompleteRead
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm guessing this # type: ignore is temporary? Should we start by typing urllib3.packages.six per the dependency tree?

@savarin
Copy link
Contributor Author

savarin commented Aug 23, 2020

Thanks for this, here are some comments for you :)

I'm wondering about the empty .pyi files, should we follow the dependency tree to commit type stubs for all the dependencies of a node before that node itself to not run into issues?

Thank you for the comments! I think that resolves all the generic Any and type ignores.

I admit I took the easy way out with six.pyi, but what the type stubs would look like there complete baffles me. I took another quick look, if understand correctly we just need to stub out MovedModule and MovedAttribute?

@savarin
Copy link
Contributor Author

savarin commented Aug 23, 2020

@sethmlarson OK I took a stab at packages.six.pyi (as I understand it six resolves which module to use), it doesn't appear we need one for packages.__init__.pyi.

@savarin
Copy link
Contributor Author

savarin commented Aug 26, 2020

@sethmlarson My initial plan was to ping to see if you had any additional thoughts, but instead I added additional downstream files to TYPED_FILES to test out packages.six.pyi. Since there errors were raised, this shows that the stubs in packages.six.pyi is not having the desired effect.

I'll take another look tomorrow. The ones that I'm still a bit puzzled on how to stub are the ones like urllib3.packages.six.moves and urllib3.packages.six.moves.urllib.parse.

"src/urllib3/util/queue.py:3: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves'"
"src/urllib3/contrib/_securetransport/bindings.py:48: error: Module 'urllib3.packages.six' has no attribute 'raise_from'"
"src/urllib3/filepost.py:9: error: Module 'urllib3.packages.six' has no attribute 'b'"
"src/urllib3/request.py:4: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves.urllib.parse'"
"src/urllib3/_collections.py:21: error: Module 'urllib3.packages.six' has no attribute 'iterkeys'"
"src/urllib3/_collections.py:21: error: Module 'urllib3.packages.six' has no attribute 'itervalues'"
"src/urllib3/_collections.py:21: error: Module 'urllib3.packages.six' has no attribute 'PY3'"
"src/urllib3/util/response.py:2: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves'"
"src/urllib3/util/request.py:4: error: Module 'urllib3.packages.six' has no attribute 'b'"
"src/urllib3/util/request.py:4: error: Module 'urllib3.packages.six' has no attribute 'integer_types'"
"src/urllib3/connection.py:10: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves.http_client'"
"src/urllib3/response.py:26: error: Module 'urllib3.packages.six' has no attribute 'string_types'"
"src/urllib3/response.py:26: error: Module 'urllib3.packages.six' has no attribute 'PY3'"
"src/urllib3/connectionpool.py:28: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves'"
"src/urllib3/poolmanager.py:20: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves.urllib.parse'"
"src/urllib3/contrib/appengine.py:45: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves.urllib.parse'"
"src/urllib3/contrib/ntlmpool.py:12: error: Cannot find implementation or library stub for module named 'urllib3.packages.six.moves.http_client'"

@savarin
Copy link
Contributor Author

savarin commented Sep 9, 2020

Merged as #1937 instead.

@savarin savarin closed this Sep 9, 2020
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.

None yet

2 participants