Skip to content

Commit 85208da

Browse files
authored
chore: Bump mypy and fix abstract ContextManager typing (getsentry#1421)
1 parent 8501874 commit 85208da

15 files changed

+35
-26
lines changed

linter-requirements.txt

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
black==22.3.0
22
flake8==3.9.2
33
flake8-import-order==0.18.1
4-
mypy==0.782
4+
mypy==0.950
5+
types-certifi
6+
types-redis
7+
types-setuptools
58
flake8-bugbear==21.4.3
69
pep8-naming==0.11.1
7-
pre-commit # local linting
10+
pre-commit # local linting

mypy.ini

+2
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,5 @@ ignore_missing_imports = True
6161
disallow_untyped_defs = False
6262
[mypy-celery.app.trace]
6363
ignore_missing_imports = True
64+
[mypy-flask.signals]
65+
ignore_missing_imports = True

sentry_sdk/hub.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def _init(*args, **kwargs):
117117
# Use `ClientConstructor` to define the argument types of `init` and
118118
# `ContextManager[Any]` to tell static analyzers about the return type.
119119

120-
class init(ClientConstructor, ContextManager[Any]): # noqa: N801
120+
class init(ClientConstructor, _InitGuard): # noqa: N801
121121
pass
122122

123123
else:

sentry_sdk/integrations/aws_lambda.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -302,12 +302,12 @@ def get_lambda_bootstrap():
302302
module = sys.modules["__main__"]
303303
# python3.9 runtime
304304
if hasattr(module, "awslambdaricmain") and hasattr(
305-
module.awslambdaricmain, "bootstrap" # type: ignore
305+
module.awslambdaricmain, "bootstrap"
306306
):
307-
return module.awslambdaricmain.bootstrap # type: ignore
307+
return module.awslambdaricmain.bootstrap
308308
elif hasattr(module, "bootstrap"):
309309
# awslambdaric python module in container builds
310-
return module.bootstrap # type: ignore
310+
return module.bootstrap
311311

312312
# python3.8 runtime
313313
return module

sentry_sdk/integrations/celery.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424

2525
try:
26-
from celery import VERSION as CELERY_VERSION # type: ignore
26+
from celery import VERSION as CELERY_VERSION
2727
from celery.exceptions import ( # type: ignore
2828
SoftTimeLimitExceeded,
2929
Retry,

sentry_sdk/integrations/excepthook.py

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,12 @@
1010
from typing import Callable
1111
from typing import Any
1212
from typing import Type
13+
from typing import Optional
1314

1415
from types import TracebackType
1516

1617
Excepthook = Callable[
17-
[Type[BaseException], BaseException, TracebackType],
18+
[Type[BaseException], BaseException, Optional[TracebackType]],
1819
Any,
1920
]
2021

@@ -43,7 +44,7 @@ def setup_once():
4344
def _make_excepthook(old_excepthook):
4445
# type: (Excepthook) -> Excepthook
4546
def sentry_sdk_excepthook(type_, value, traceback):
46-
# type: (Type[BaseException], BaseException, TracebackType) -> None
47+
# type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None
4748
hub = Hub.current
4849
integration = hub.get_integration(ExcepthookIntegration)
4950

sentry_sdk/integrations/flask.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def sentry_patched_wsgi_app(self, environ, start_response):
9494
environ, start_response
9595
)
9696

97-
Flask.__call__ = sentry_patched_wsgi_app # type: ignore
97+
Flask.__call__ = sentry_patched_wsgi_app
9898

9999

100100
def _add_sentry_trace(sender, template, context, **extra):

sentry_sdk/integrations/gcp.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ def __init__(self, timeout_warning=False):
126126
@staticmethod
127127
def setup_once():
128128
# type: () -> None
129-
import __main__ as gcp_functions # type: ignore
129+
import __main__ as gcp_functions
130130

131131
if not hasattr(gcp_functions, "worker_v1"):
132132
logger.warning(

sentry_sdk/integrations/logging.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ def _handle_record(self, record):
7878
@staticmethod
7979
def setup_once():
8080
# type: () -> None
81-
old_callhandlers = logging.Logger.callHandlers # type: ignore
81+
old_callhandlers = logging.Logger.callHandlers
8282

8383
def sentry_patched_callhandlers(self, record):
8484
# type: (Any, LogRecord) -> Any

sentry_sdk/integrations/sqlalchemy.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
7070
# type: (Any, Any, Any, Any, Any, *Any) -> None
7171
ctx_mgr = getattr(
7272
context, "_sentry_sql_span_manager", None
73-
) # type: ContextManager[Any]
73+
) # type: Optional[ContextManager[Any]]
7474

7575
if ctx_mgr is not None:
7676
context._sentry_sql_span_manager = None
@@ -93,7 +93,7 @@ def _handle_error(context, *args):
9393
# handler is going to be fatal.
9494
ctx_mgr = getattr(
9595
execution_context, "_sentry_sql_span_manager", None
96-
) # type: ContextManager[Any]
96+
) # type: Optional[ContextManager[Any]]
9797

9898
if ctx_mgr is not None:
9999
execution_context._sentry_sql_span_manager = None

sentry_sdk/integrations/stdlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ def sentry_patched_popen_init(self, *a, **kw):
157157

158158
hub = Hub.current
159159
if hub.get_integration(StdlibIntegration) is None:
160-
return old_popen_init(self, *a, **kw) # type: ignore
160+
return old_popen_init(self, *a, **kw)
161161

162162
# Convert from tuple to list to be able to set values.
163163
a = list(a)
@@ -195,7 +195,7 @@ def sentry_patched_popen_init(self, *a, **kw):
195195
if cwd:
196196
span.set_data("subprocess.cwd", cwd)
197197

198-
rv = old_popen_init(self, *a, **kw) # type: ignore
198+
rv = old_popen_init(self, *a, **kw)
199199

200200
span.set_tag("subprocess.pid", self.pid)
201201
return rv

sentry_sdk/integrations/threading.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def sentry_start(self, *a, **kw):
5151
new_run = _wrap_run(hub_, getattr(self.run, "__func__", self.run))
5252
self.run = new_run # type: ignore
5353

54-
return old_start(self, *a, **kw) # type: ignore
54+
return old_start(self, *a, **kw)
5555

5656
Thread.start = sentry_start # type: ignore
5757

sentry_sdk/integrations/tornado.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from sentry_sdk._compat import iteritems
2222

2323
try:
24-
from tornado import version_info as TORNADO_VERSION # type: ignore
24+
from tornado import version_info as TORNADO_VERSION
2525
from tornado.web import RequestHandler, HTTPError
2626
from tornado.gen import coroutine
2727
except ImportError:
@@ -58,7 +58,7 @@ def setup_once():
5858

5959
ignore_logger("tornado.access")
6060

61-
old_execute = RequestHandler._execute # type: ignore
61+
old_execute = RequestHandler._execute
6262

6363
awaitable = iscoroutinefunction(old_execute)
6464

@@ -79,16 +79,16 @@ def sentry_execute_request_handler(self, *args, **kwargs): # type: ignore
7979
result = yield from old_execute(self, *args, **kwargs)
8080
return result
8181

82-
RequestHandler._execute = sentry_execute_request_handler # type: ignore
82+
RequestHandler._execute = sentry_execute_request_handler
8383

8484
old_log_exception = RequestHandler.log_exception
8585

8686
def sentry_log_exception(self, ty, value, tb, *args, **kwargs):
8787
# type: (Any, type, BaseException, Any, *Any, **Any) -> Optional[Any]
8888
_capture_exception(ty, value, tb)
89-
return old_log_exception(self, ty, value, tb, *args, **kwargs) # type: ignore
89+
return old_log_exception(self, ty, value, tb, *args, **kwargs)
9090

91-
RequestHandler.log_exception = sentry_log_exception # type: ignore
91+
RequestHandler.log_exception = sentry_log_exception
9292

9393

9494
@contextlib.contextmanager
@@ -105,7 +105,7 @@ def _handle_request_impl(self):
105105
with Hub(hub) as hub:
106106
with hub.configure_scope() as scope:
107107
scope.clear_breadcrumbs()
108-
processor = _make_event_processor(weak_handler) # type: ignore
108+
processor = _make_event_processor(weak_handler)
109109
scope.add_event_processor(processor)
110110

111111
transaction = Transaction.continue_from_headers(
@@ -155,7 +155,7 @@ def tornado_processor(event, hint):
155155
request = handler.request
156156

157157
with capture_internal_exceptions():
158-
method = getattr(handler, handler.request.method.lower()) # type: ignore
158+
method = getattr(handler, handler.request.method.lower())
159159
event["transaction"] = transaction_from_function(method)
160160

161161
with capture_internal_exceptions():

sentry_sdk/utils.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ def __init__(self, value):
171171
self.host = parts.hostname
172172

173173
if parts.port is None:
174-
self.port = self.scheme == "https" and 443 or 80
174+
self.port = self.scheme == "https" and 443 or 80 # type: int
175175
else:
176176
self.port = parts.port
177177

@@ -466,6 +466,9 @@ def filename_for_module(module, abs_path):
466466
return os.path.basename(abs_path)
467467

468468
base_module_path = sys.modules[base_module].__file__
469+
if not base_module_path:
470+
return abs_path
471+
469472
return abs_path.split(base_module_path.rsplit(os.sep, 2)[0], 1)[-1].lstrip(
470473
os.sep
471474
)

tox.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -324,4 +324,4 @@ commands =
324324
commands =
325325
flake8 tests examples sentry_sdk
326326
black --check tests examples sentry_sdk
327-
mypy examples sentry_sdk
327+
mypy sentry_sdk

0 commit comments

Comments
 (0)