Skip to content

Commit 7ea6fd3

Browse files
committed
fix ty check for conditional importlib_metadata import
1 parent 084fdaa commit 7ea6fd3

4 files changed

Lines changed: 91 additions & 1 deletion

File tree

pyproject.toml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,12 @@ ignore = [
214214
[tool.ruff.lint.isort]
215215
force-single-line = false
216216

217+
[tool.ty.lint]
218+
# Needed because importlib_metadata is only installed on Python < 3.10,
219+
# but we need ty:ignore for when ty runs on Python >= 3.10
220+
ignore = ["unused-ignore-comment"]
221+
222+
217223
[tool.uv]
218224
package = true
219225
default-groups = "all"

scripts/example_requests.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/usr/bin/env -S uv run --script
2+
# /// script
3+
# dependencies = [
4+
# "httpclient-logging",
5+
# "requests",
6+
# "rich",
7+
# ]
8+
# ///
9+
10+
"""Demo requests usage."""
11+
12+
import logging
13+
14+
import requests
15+
import rich
16+
import rich.logging
17+
18+
# configure colorized logging to console
19+
# enables output from all loggers including http.client
20+
logging.basicConfig(
21+
level=logging.DEBUG,
22+
format="%(name)s - %(message)s",
23+
handlers=[rich.logging.RichHandler(rich_tracebacks=False)],
24+
)
25+
26+
# experiment with different levels
27+
# logging.getLogger("example").setLevel(logging.INFO)
28+
# logging.getLogger("http.client").setLevel(logging.INFO)
29+
# logging.getLogger("urllib3").setLevel(logging.INFO)
30+
31+
log = logging.getLogger("example")
32+
33+
url = "https://peps.python.org/api/peps.json"
34+
35+
log.info(f"Fetching data from {url}")
36+
resp = requests.get(url, timeout=5)
37+
log.info("Requests response status code: %s", resp.status_code)
38+
data = resp.json()
39+
40+
rich.print([(k, v["title"]) for k, v in data.items()][:10])

scripts/example_urllib.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
#!/usr/bin/env -S uv run --script
2+
# /// script
3+
# dependencies = [
4+
# "httpclient-logging",
5+
# "libranet-logging",
6+
# "requests",
7+
# "rich",
8+
# ]
9+
# ///
10+
11+
"""Demo requests usage."""
12+
13+
import json
14+
import logging
15+
import urllib.request
16+
17+
# configure colorized logging to console
18+
# enables output from all loggers including http.client
19+
# logging.basicConfig(
20+
# level=logging.DEBUG,
21+
# format="%(name)s - %(message)s",
22+
# handlers=[rich.logging.RichHandler(rich_tracebacks=False)],
23+
# )
24+
import libranet_logging
25+
import rich
26+
import rich.logging
27+
28+
libranet_logging.initialize()
29+
30+
# experiment with different levels
31+
# logging.getLogger("example").setLevel(logging.INFO)
32+
# logging.getLogger("http.client").setLevel(logging.INFO)
33+
# logging.getLogger("urllib3").setLevel(logging.INFO)
34+
35+
log = logging.getLogger("example")
36+
37+
url = "https://peps.python.org/api/peps.json"
38+
39+
log.info(f"Fetching data from {url}")
40+
resp = urllib.request.urlopen(url, timeout=5)
41+
log.info("Requests response status code: %s", resp.code)
42+
data = json.load(resp)
43+
44+
rich.print([(k, v["title"]) for k, v in data.items()][:10])

src/httpclient_logging/about.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
if sys.version_info >= (3, 10):
1313
import importlib.metadata as importlib_metadata
1414
else:
15-
import importlib_metadata
15+
import importlib_metadata # ty:ignore[unresolved-import]
1616

1717
PACKAGE: str = __package__ or ""
1818

0 commit comments

Comments
 (0)