Skip to content

Commit 71c5f40

Browse files
Daverballantonpirker
authored andcommitted
ref(typing): Add additional stub packages for type checking (#3122)
Adds `types-webob`, `types-greenlet` and `types-gevent` to linter requirements and fixes newly exposed typing issues.
1 parent 51ebf40 commit 71c5f40

File tree

7 files changed

+33
-20
lines changed

7 files changed

+33
-20
lines changed

requirements-docs.txt

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
gevent
12
shibuya
23
sphinx==7.2.6
34
sphinx-autodoc-typehints[type_comments]>=1.8.0

requirements-linting.txt

+3
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,11 @@ black
33
flake8==5.0.4 # flake8 depends on pyflakes>=3.0.0 and this dropped support for Python 2 "# type:" comments
44
types-certifi
55
types-protobuf
6+
types-gevent
7+
types-greenlet
68
types-redis
79
types-setuptools
10+
types-webob
811
pymongo # There is no separate types module.
912
loguru # There is no separate types module.
1013
flake8-bugbear

sentry_sdk/integrations/_wsgi_common.py

+2-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from typing import Any
1717
from typing import Dict
1818
from typing import Mapping
19+
from typing import MutableMapping
1920
from typing import Optional
2021
from typing import Union
2122
from sentry_sdk._types import Event, HttpStatusCodeRange
@@ -114,7 +115,7 @@ def content_length(self):
114115
return 0
115116

116117
def cookies(self):
117-
# type: () -> Dict[str, Any]
118+
# type: () -> MutableMapping[str, Any]
118119
raise NotImplementedError()
119120

120121
def raw_data(self):

sentry_sdk/integrations/pyramid.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
from typing import Callable
3131
from typing import Dict
3232
from typing import Optional
33-
from webob.cookies import RequestCookies # type: ignore
34-
from webob.compat import cgi_FieldStorage # type: ignore
33+
from webob.cookies import RequestCookies
34+
from webob.request import _FieldStorageWithFile
3535

3636
from sentry_sdk.utils import ExcInfo
3737
from sentry_sdk._types import Event, EventProcessor
@@ -189,15 +189,15 @@ def form(self):
189189
}
190190

191191
def files(self):
192-
# type: () -> Dict[str, cgi_FieldStorage]
192+
# type: () -> Dict[str, _FieldStorageWithFile]
193193
return {
194194
key: value
195195
for key, value in self.request.POST.items()
196196
if getattr(value, "filename", None)
197197
}
198198

199199
def size_of_file(self, postdata):
200-
# type: (cgi_FieldStorage) -> int
200+
# type: (_FieldStorageWithFile) -> int
201201
file = postdata.file
202202
try:
203203
return os.fstat(file.fileno()).st_size

sentry_sdk/profiler/continuous_profiler.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
from typing import Dict
2929
from typing import List
3030
from typing import Optional
31+
from typing import Type
3132
from typing import Union
3233
from typing_extensions import TypedDict
3334
from sentry_sdk._types import ContinuousProfilerMode
@@ -51,9 +52,10 @@
5152

5253

5354
try:
54-
from gevent.monkey import get_original # type: ignore
55-
from gevent.threadpool import ThreadPool # type: ignore
55+
from gevent.monkey import get_original
56+
from gevent.threadpool import ThreadPool as _ThreadPool
5657

58+
ThreadPool = _ThreadPool # type: Optional[Type[_ThreadPool]]
5759
thread_sleep = get_original("time", "sleep")
5860
except ImportError:
5961
thread_sleep = time.sleep
@@ -347,7 +349,7 @@ def __init__(self, frequency, options, capture_func):
347349

348350
super().__init__(frequency, options, capture_func)
349351

350-
self.thread = None # type: Optional[ThreadPool]
352+
self.thread = None # type: Optional[_ThreadPool]
351353
self.pid = None # type: Optional[int]
352354
self.lock = threading.Lock()
353355

@@ -377,7 +379,7 @@ def ensure_running(self):
377379
# we should create a new buffer along with it
378380
self.reset_buffer()
379381

380-
self.thread = ThreadPool(1)
382+
self.thread = ThreadPool(1) # type: ignore[misc]
381383
try:
382384
self.thread.spawn(self.run)
383385
except RuntimeError:

sentry_sdk/profiler/transaction_profiler.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
from typing import List
6262
from typing import Optional
6363
from typing import Set
64+
from typing import Type
6465
from typing_extensions import TypedDict
6566

6667
from sentry_sdk.profiler.utils import (
@@ -95,9 +96,10 @@
9596

9697

9798
try:
98-
from gevent.monkey import get_original # type: ignore
99-
from gevent.threadpool import ThreadPool # type: ignore
99+
from gevent.monkey import get_original
100+
from gevent.threadpool import ThreadPool as _ThreadPool
100101

102+
ThreadPool = _ThreadPool # type: Optional[Type[_ThreadPool]]
101103
thread_sleep = get_original("time", "sleep")
102104
except ImportError:
103105
thread_sleep = time.sleep
@@ -738,7 +740,7 @@ def __init__(self, frequency):
738740

739741
# used to signal to the thread that it should stop
740742
self.running = False
741-
self.thread = None # type: Optional[ThreadPool]
743+
self.thread = None # type: Optional[_ThreadPool]
742744
self.pid = None # type: Optional[int]
743745

744746
# This intentionally uses the gevent patched threading.Lock.
@@ -775,7 +777,7 @@ def ensure_running(self):
775777
self.pid = pid
776778
self.running = True
777779

778-
self.thread = ThreadPool(1)
780+
self.thread = ThreadPool(1) # type: ignore[misc]
779781
try:
780782
self.thread.spawn(self.run)
781783
except RuntimeError:

sentry_sdk/utils.py

+11-7
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,8 @@
5454
Union,
5555
)
5656

57+
from gevent.hub import Hub
58+
5759
import sentry_sdk.integrations
5860
from sentry_sdk._types import Event, ExcInfo
5961

@@ -1182,8 +1184,8 @@ def _is_contextvars_broken():
11821184
Returns whether gevent/eventlet have patched the stdlib in a way where thread locals are now more "correct" than contextvars.
11831185
"""
11841186
try:
1185-
import gevent # type: ignore
1186-
from gevent.monkey import is_object_patched # type: ignore
1187+
import gevent
1188+
from gevent.monkey import is_object_patched
11871189

11881190
# Get the MAJOR and MINOR version numbers of Gevent
11891191
version_tuple = tuple(
@@ -1209,7 +1211,7 @@ def _is_contextvars_broken():
12091211
pass
12101212

12111213
try:
1212-
import greenlet # type: ignore
1214+
import greenlet
12131215
from eventlet.patcher import is_monkey_patched # type: ignore
12141216

12151217
greenlet_version = parse_version(greenlet.__version__)
@@ -1794,12 +1796,14 @@ def now():
17941796
from gevent.monkey import is_module_patched
17951797
except ImportError:
17961798

1797-
def get_gevent_hub():
1798-
# type: () -> Any
1799+
# it's not great that the signatures are different, get_hub can't return None
1800+
# consider adding an if TYPE_CHECKING to change the signature to Optional[Hub]
1801+
def get_gevent_hub(): # type: ignore[misc]
1802+
# type: () -> Optional[Hub]
17991803
return None
18001804

1801-
def is_module_patched(*args, **kwargs):
1802-
# type: (*Any, **Any) -> bool
1805+
def is_module_patched(mod_name):
1806+
# type: (str) -> bool
18031807
# unable to import from gevent means no modules have been patched
18041808
return False
18051809

0 commit comments

Comments
 (0)