diff --git a/linter-requirements.txt b/linter-requirements.txt index 744904fbc2..ec736a59c5 100644 --- a/linter-requirements.txt +++ b/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 \ No newline at end of file +pre-commit # local linting diff --git a/mypy.ini b/mypy.ini index 7e30dddb5b..2a15e45e49 100644 --- a/mypy.ini +++ b/mypy.ini @@ -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 diff --git a/sentry_sdk/hub.py b/sentry_sdk/hub.py index 22f3ff42fd..d2b57a2e45 100644 --- a/sentry_sdk/hub.py +++ b/sentry_sdk/hub.py @@ -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. - class init(ClientConstructor, ContextManager[Any]): # noqa: N801 + class init(ClientConstructor, _InitGuard): # noqa: N801 pass else: diff --git a/sentry_sdk/integrations/aws_lambda.py b/sentry_sdk/integrations/aws_lambda.py index 0eae710bff..10b5025abe 100644 --- a/sentry_sdk/integrations/aws_lambda.py +++ b/sentry_sdk/integrations/aws_lambda.py @@ -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 diff --git a/sentry_sdk/integrations/celery.py b/sentry_sdk/integrations/celery.py index 40a2dfbe39..743e2cfb50 100644 --- a/sentry_sdk/integrations/celery.py +++ b/sentry_sdk/integrations/celery.py @@ -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, diff --git a/sentry_sdk/integrations/excepthook.py b/sentry_sdk/integrations/excepthook.py index 1e8597e13f..1f16ff0b06 100644 --- a/sentry_sdk/integrations/excepthook.py +++ b/sentry_sdk/integrations/excepthook.py @@ -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, ] @@ -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) diff --git a/sentry_sdk/integrations/flask.py b/sentry_sdk/integrations/flask.py index 8883cbb724..5aade50a94 100644 --- a/sentry_sdk/integrations/flask.py +++ b/sentry_sdk/integrations/flask.py @@ -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): diff --git a/sentry_sdk/integrations/gcp.py b/sentry_sdk/integrations/gcp.py index e92422d8b9..118970e9d8 100644 --- a/sentry_sdk/integrations/gcp.py +++ b/sentry_sdk/integrations/gcp.py @@ -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( diff --git a/sentry_sdk/integrations/logging.py b/sentry_sdk/integrations/logging.py index 31c7b874ba..e9f3fe9dbb 100644 --- a/sentry_sdk/integrations/logging.py +++ b/sentry_sdk/integrations/logging.py @@ -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 diff --git a/sentry_sdk/integrations/sqlalchemy.py b/sentry_sdk/integrations/sqlalchemy.py index 3d10f2041e..deb97c05ad 100644 --- a/sentry_sdk/integrations/sqlalchemy.py +++ b/sentry_sdk/integrations/sqlalchemy.py @@ -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 @@ -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 diff --git a/sentry_sdk/integrations/stdlib.py b/sentry_sdk/integrations/stdlib.py index adea742b2d..9495d406dc 100644 --- a/sentry_sdk/integrations/stdlib.py +++ b/sentry_sdk/integrations/stdlib.py @@ -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) @@ -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 diff --git a/sentry_sdk/integrations/threading.py b/sentry_sdk/integrations/threading.py index b750257e2a..f29e5e8797 100644 --- a/sentry_sdk/integrations/threading.py +++ b/sentry_sdk/integrations/threading.py @@ -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 diff --git a/sentry_sdk/integrations/tornado.py b/sentry_sdk/integrations/tornado.py index f9796daca3..443ebefaa8 100644 --- a/sentry_sdk/integrations/tornado.py +++ b/sentry_sdk/integrations/tornado.py @@ -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: @@ -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) @@ -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 @@ -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( @@ -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(): diff --git a/sentry_sdk/utils.py b/sentry_sdk/utils.py index e22f6ae065..0a735a1e20 100644 --- a/sentry_sdk/utils.py +++ b/sentry_sdk/utils.py @@ -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 @@ -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 ) diff --git a/tox.ini b/tox.ini index 2cdf8a45bf..0ca43ab8a2 100644 --- a/tox.ini +++ b/tox.ini @@ -324,4 +324,4 @@ commands = commands = flake8 tests examples sentry_sdk black --check tests examples sentry_sdk - mypy examples sentry_sdk + mypy sentry_sdk