Skip to content

Commit 100da78

Browse files
committed
ref(typing): Add additional stub packages for type checking
Adds `types-webob`, `types-greenlet` and `types-gevent` to linter requirements and fixes newly exposed typing issues. Fixes GH-2226
1 parent c80cad1 commit 100da78

File tree

5 files changed

+26
-16
lines changed

5 files changed

+26
-16
lines changed

Diff for: linter-requirements.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

Diff for: 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
@@ -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):

Diff for: 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
@@ -186,15 +186,15 @@ def form(self):
186186
}
187187

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

196196
def size_of_file(self, postdata):
197-
# type: (cgi_FieldStorage) -> int
197+
# type: (_FieldStorageWithFile) -> int
198198
file = postdata.file
199199
try:
200200
return os.fstat(file.fileno()).st_size

Diff for: sentry_sdk/profiler.py

+6-4
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
from typing import Set
6363
from typing import Sequence
6464
from typing import Tuple
65+
from typing import Type
6566
from typing_extensions import TypedDict
6667

6768
import sentry_sdk.tracing
@@ -129,9 +130,10 @@
129130

130131

131132
try:
132-
from gevent.monkey import get_original # type: ignore
133-
from gevent.threadpool import ThreadPool # type: ignore
133+
from gevent.monkey import get_original
134+
from gevent.threadpool import ThreadPool as _ThreadPool
134135

136+
ThreadPool = _ThreadPool # type: Optional[Type[_ThreadPool]]
135137
thread_sleep = get_original("time", "sleep")
136138
except ImportError:
137139
thread_sleep = time.sleep
@@ -923,7 +925,7 @@ def __init__(self, frequency):
923925

924926
# used to signal to the thread that it should stop
925927
self.running = False
926-
self.thread = None # type: Optional[ThreadPool]
928+
self.thread = None # type: Optional[_ThreadPool]
927929
self.pid = None # type: Optional[int]
928930

929931
# This intentionally uses the gevent patched threading.Lock.
@@ -960,7 +962,7 @@ def ensure_running(self):
960962
self.pid = pid
961963
self.running = True
962964

963-
self.thread = ThreadPool(1)
965+
self.thread = ThreadPool(1) # type: ignore[misc]
964966
try:
965967
self.thread.spawn(self.run)
966968
except RuntimeError:

Diff for: 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)