Skip to content

Commit

Permalink
Remove and dissallow unused type ignores (#2470)
Browse files Browse the repository at this point in the history
* Remove and dissallow unused type ignores

* add pragmas

* fix ignore
  • Loading branch information
adriangb committed Nov 29, 2022
1 parent 1fc6a52 commit 69e13cb
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 24 deletions.
16 changes: 9 additions & 7 deletions httpx/_config.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import os
import ssl
import sys
import typing
from pathlib import Path

Expand Down Expand Up @@ -125,15 +126,16 @@ def load_ssl_context_verify(self) -> ssl.SSLContext:

# Signal to server support for PHA in TLS 1.3. Raises an
# AttributeError if only read-only access is implemented.
try:
context.post_handshake_auth = True # type: ignore
except AttributeError: # pragma: no cover
pass
if sys.version_info >= (3, 8): # pragma: no cover
try:
context.post_handshake_auth = True
except AttributeError: # pragma: no cover
pass

# Disable using 'commonName' for SSLContext.check_hostname
# when the 'subjectAltName' extension isn't available.
try:
context.hostname_checks_common_name = False # type: ignore
context.hostname_checks_common_name = False
except AttributeError: # pragma: no cover
pass

Expand Down Expand Up @@ -162,10 +164,10 @@ def _create_default_ssl_context(self) -> ssl.SSLContext:
alpn_idents = ["http/1.1", "h2"] if self.http2 else ["http/1.1"]
context.set_alpn_protocols(alpn_idents)

if hasattr(context, "keylog_filename"): # pragma: no cover (Available in 3.8+)
if sys.version_info >= (3, 8): # pragma: no cover
keylogfile = os.environ.get("SSLKEYLOGFILE")
if keylogfile and self.trust_env:
context.keylog_filename = keylogfile # type: ignore
context.keylog_filename = keylogfile

return context

Expand Down
2 changes: 1 addition & 1 deletion httpx/_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def encode_request(
Handles encoding the given `content`, `data`, `files`, and `json`,
returning a two-tuple of (<headers>, <stream>).
"""
if data is not None and not isinstance(data, Mapping): # type: ignore
if data is not None and not isinstance(data, Mapping):
# We prefer to separate `content=<bytes|str|byte iterator|bytes aiterator>`
# for raw request content, and `data=<form data>` for url encoded or
# multipart form content.
Expand Down
4 changes: 2 additions & 2 deletions httpx/_status_codes.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ class codes(IntEnum):
"""

def __new__(cls, value: int, phrase: str = "") -> "codes":
obj = int.__new__(cls, value) # type: ignore
obj = int.__new__(cls, value)
obj._value_ = value

obj.phrase = phrase # type: ignore
obj.phrase = phrase # type: ignore[attr-defined]
return obj

def __str__(self) -> str:
Expand Down
2 changes: 1 addition & 1 deletion httpx/_transports/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,6 @@ async def handle_async_request(

# https://simonwillison.net/2020/Sep/2/await-me-maybe/
if asyncio.iscoroutine(response):
response = await response # type: ignore[func-returns-value,assignment]
response = await response

return response
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ disallow_any_generics = True
ignore_missing_imports = True
no_implicit_optional = True
show_error_codes = True
warn_unused_ignores = True
warn_unused_configs = True
disallow_subclassing_any = True
check_untyped_defs = True
Expand Down
23 changes: 12 additions & 11 deletions tests/test_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,26 @@ def test_timeout_repr():
)
@pytest.mark.skipif(sys.version_info < (3, 8), reason="requires python3.8 or higher")
def test_ssl_config_support_for_keylog_file(tmpdir, monkeypatch): # pragma: no cover
with monkeypatch.context() as m:
m.delenv("SSLKEYLOGFILE", raising=False)
if sys.version_info > (3, 8):
with monkeypatch.context() as m:
m.delenv("SSLKEYLOGFILE", raising=False)

context = httpx.create_ssl_context(trust_env=True)
context = httpx.create_ssl_context(trust_env=True)

assert context.keylog_filename is None # type: ignore
assert context.keylog_filename is None

filename = str(tmpdir.join("test.log"))
filename = str(tmpdir.join("test.log"))

with monkeypatch.context() as m:
m.setenv("SSLKEYLOGFILE", filename)
with monkeypatch.context() as m:
m.setenv("SSLKEYLOGFILE", filename)

context = httpx.create_ssl_context(trust_env=True)
context = httpx.create_ssl_context(trust_env=True)

assert context.keylog_filename == filename # type: ignore
assert context.keylog_filename == filename

context = httpx.create_ssl_context(trust_env=False)
context = httpx.create_ssl_context(trust_env=False)

assert context.keylog_filename is None # type: ignore
assert context.keylog_filename is None


def test_proxy_from_url():
Expand Down
4 changes: 2 additions & 2 deletions tests/test_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ def hello_world():

# Support 'data' for compat with requests.
with pytest.warns(DeprecationWarning):
headers, stream = encode_request(data=hello_world()) # type: ignore
headers, stream = encode_request(data=hello_world())
assert isinstance(stream, typing.Iterable)
assert not isinstance(stream, typing.AsyncIterable)

Expand Down Expand Up @@ -135,7 +135,7 @@ async def hello_world():

# Support 'data' for compat with requests.
with pytest.warns(DeprecationWarning):
headers, stream = encode_request(data=hello_world()) # type: ignore
headers, stream = encode_request(data=hello_world())
assert not isinstance(stream, typing.Iterable)
assert isinstance(stream, typing.AsyncIterable)

Expand Down

0 comments on commit 69e13cb

Please sign in to comment.