Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Bump mypy and fix abstract ContextManager typing #1421

Merged
merged 7 commits into from May 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 5 additions & 2 deletions linter-requirements.txt
@@ -1,7 +1,10 @@
black==22.3.0
flake8==3.9.2
flake8-import-order==0.18.1
mypy==0.782
mypy==0.950
types-certifi
types-redis
types-setuptools
flake8-bugbear==21.4.3
pep8-naming==0.11.1
pre-commit # local linting
pre-commit # local linting
2 changes: 2 additions & 0 deletions mypy.ini
Expand Up @@ -61,3 +61,5 @@ ignore_missing_imports = True
disallow_untyped_defs = False
[mypy-celery.app.trace]
ignore_missing_imports = True
[mypy-flask.signals]
ignore_missing_imports = True
2 changes: 1 addition & 1 deletion sentry_sdk/hub.py
Expand Up @@ -117,7 +117,7 @@ def _init(*args, **kwargs):
# Use `ClientConstructor` to define the argument types of `init` and
# `ContextManager[Any]` to tell static analyzers about the return type.
sl0thentr0py marked this conversation as resolved.
Show resolved Hide resolved

class init(ClientConstructor, ContextManager[Any]): # noqa: N801
class init(ClientConstructor, _InitGuard): # noqa: N801
pass

else:
Expand Down
6 changes: 3 additions & 3 deletions sentry_sdk/integrations/aws_lambda.py
Expand Up @@ -302,12 +302,12 @@ def get_lambda_bootstrap():
module = sys.modules["__main__"]
# python3.9 runtime
if hasattr(module, "awslambdaricmain") and hasattr(
module.awslambdaricmain, "bootstrap" # type: ignore
module.awslambdaricmain, "bootstrap"
):
return module.awslambdaricmain.bootstrap # type: ignore
return module.awslambdaricmain.bootstrap
elif hasattr(module, "bootstrap"):
# awslambdaric python module in container builds
return module.bootstrap # type: ignore
return module.bootstrap

# python3.8 runtime
return module
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/celery.py
Expand Up @@ -23,7 +23,7 @@


try:
from celery import VERSION as CELERY_VERSION # type: ignore
from celery import VERSION as CELERY_VERSION
from celery.exceptions import ( # type: ignore
SoftTimeLimitExceeded,
Retry,
Expand Down
5 changes: 3 additions & 2 deletions sentry_sdk/integrations/excepthook.py
Expand Up @@ -10,11 +10,12 @@
from typing import Callable
from typing import Any
from typing import Type
from typing import Optional

from types import TracebackType

Excepthook = Callable[
[Type[BaseException], BaseException, TracebackType],
[Type[BaseException], BaseException, Optional[TracebackType]],
Any,
]

Expand Down Expand Up @@ -43,7 +44,7 @@ def setup_once():
def _make_excepthook(old_excepthook):
# type: (Excepthook) -> Excepthook
def sentry_sdk_excepthook(type_, value, traceback):
# type: (Type[BaseException], BaseException, TracebackType) -> None
# type: (Type[BaseException], BaseException, Optional[TracebackType]) -> None
hub = Hub.current
integration = hub.get_integration(ExcepthookIntegration)

Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/flask.py
Expand Up @@ -94,7 +94,7 @@ def sentry_patched_wsgi_app(self, environ, start_response):
environ, start_response
)

Flask.__call__ = sentry_patched_wsgi_app # type: ignore
Flask.__call__ = sentry_patched_wsgi_app


def _add_sentry_trace(sender, template, context, **extra):
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/gcp.py
Expand Up @@ -126,7 +126,7 @@ def __init__(self, timeout_warning=False):
@staticmethod
def setup_once():
# type: () -> None
import __main__ as gcp_functions # type: ignore
import __main__ as gcp_functions

if not hasattr(gcp_functions, "worker_v1"):
logger.warning(
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/logging.py
Expand Up @@ -78,7 +78,7 @@ def _handle_record(self, record):
@staticmethod
def setup_once():
# type: () -> None
old_callhandlers = logging.Logger.callHandlers # type: ignore
old_callhandlers = logging.Logger.callHandlers

def sentry_patched_callhandlers(self, record):
# type: (Any, LogRecord) -> Any
Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/sqlalchemy.py
Expand Up @@ -70,7 +70,7 @@ def _after_cursor_execute(conn, cursor, statement, parameters, context, *args):
# type: (Any, Any, Any, Any, Any, *Any) -> None
ctx_mgr = getattr(
context, "_sentry_sql_span_manager", None
) # type: ContextManager[Any]
) # type: Optional[ContextManager[Any]]

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

if ctx_mgr is not None:
execution_context._sentry_sql_span_manager = None
Expand Down
4 changes: 2 additions & 2 deletions sentry_sdk/integrations/stdlib.py
Expand Up @@ -157,7 +157,7 @@ def sentry_patched_popen_init(self, *a, **kw):

hub = Hub.current
if hub.get_integration(StdlibIntegration) is None:
return old_popen_init(self, *a, **kw) # type: ignore
return old_popen_init(self, *a, **kw)

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

rv = old_popen_init(self, *a, **kw) # type: ignore
rv = old_popen_init(self, *a, **kw)

span.set_tag("subprocess.pid", self.pid)
return rv
Expand Down
2 changes: 1 addition & 1 deletion sentry_sdk/integrations/threading.py
Expand Up @@ -51,7 +51,7 @@ def sentry_start(self, *a, **kw):
new_run = _wrap_run(hub_, getattr(self.run, "__func__", self.run))
self.run = new_run # type: ignore

return old_start(self, *a, **kw) # type: ignore
return old_start(self, *a, **kw)

Thread.start = sentry_start # type: ignore

Expand Down
14 changes: 7 additions & 7 deletions sentry_sdk/integrations/tornado.py
Expand Up @@ -21,7 +21,7 @@
from sentry_sdk._compat import iteritems

try:
from tornado import version_info as TORNADO_VERSION # type: ignore
from tornado import version_info as TORNADO_VERSION
from tornado.web import RequestHandler, HTTPError
from tornado.gen import coroutine
except ImportError:
Expand Down Expand Up @@ -58,7 +58,7 @@ def setup_once():

ignore_logger("tornado.access")

old_execute = RequestHandler._execute # type: ignore
old_execute = RequestHandler._execute

awaitable = iscoroutinefunction(old_execute)

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

RequestHandler._execute = sentry_execute_request_handler # type: ignore
RequestHandler._execute = sentry_execute_request_handler

old_log_exception = RequestHandler.log_exception

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

RequestHandler.log_exception = sentry_log_exception # type: ignore
RequestHandler.log_exception = sentry_log_exception


@contextlib.contextmanager
Expand All @@ -105,7 +105,7 @@ def _handle_request_impl(self):
with Hub(hub) as hub:
with hub.configure_scope() as scope:
scope.clear_breadcrumbs()
processor = _make_event_processor(weak_handler) # type: ignore
processor = _make_event_processor(weak_handler)
scope.add_event_processor(processor)

transaction = Transaction.continue_from_headers(
Expand Down Expand Up @@ -155,7 +155,7 @@ def tornado_processor(event, hint):
request = handler.request

with capture_internal_exceptions():
method = getattr(handler, handler.request.method.lower()) # type: ignore
method = getattr(handler, handler.request.method.lower())
event["transaction"] = transaction_from_function(method)

with capture_internal_exceptions():
Expand Down
5 changes: 4 additions & 1 deletion sentry_sdk/utils.py
Expand Up @@ -171,7 +171,7 @@ def __init__(self, value):
self.host = parts.hostname

if parts.port is None:
self.port = self.scheme == "https" and 443 or 80
self.port = self.scheme == "https" and 443 or 80 # type: int
else:
self.port = parts.port

Expand Down Expand Up @@ -466,6 +466,9 @@ def filename_for_module(module, abs_path):
return os.path.basename(abs_path)

base_module_path = sys.modules[base_module].__file__
if not base_module_path:
return abs_path

return abs_path.split(base_module_path.rsplit(os.sep, 2)[0], 1)[-1].lstrip(
os.sep
)
Expand Down
2 changes: 1 addition & 1 deletion tox.ini
Expand Up @@ -324,4 +324,4 @@ commands =
commands =
flake8 tests examples sentry_sdk
black --check tests examples sentry_sdk
mypy examples sentry_sdk
mypy sentry_sdk