From d435e9c089517837e708788a427513ae8b84f89f Mon Sep 17 00:00:00 2001 From: Dan Callahan Date: Wed, 10 Nov 2021 21:12:07 +0000 Subject: [PATCH] Move mypy configuration into pyproject.toml This is mainly to take advantage of the significantly more convenient syntax for module overrides. For example, instead of: [mypy-synapse.api.*] disallow_untyped_defs = True [mypy-synapse.app.*] disallow_untyped_defs = True [mypy-synapse.crypto.*] disallow_untyped_defs = True The pyproject.toml file allows: [[tool.mypy.overrides]] module = [ "synapse.api.*", "synapse.app.*", "synapse.crypto.*", ] disallow_untyped_defs = true Not a huge difference with two or three modules, but we list many more. This is especially important as I'm about to set disallow_untyped_defs at the global level and provide module-level opt-outs so that we require typed definitions in all newly added code. Switching to this format will save several hundred lines of repetitive boilerplate and clarify the intent of each stanza. It's also easier to keep things ordered, since it works to just pipe a region to `sort`, while mypy.ini requires handling a 3-line structure. Ordering helps keep these settings synced up with the files on disk. Signed-off-by: Dan Callahan --- changelog.d/11315.misc | 1 + mypy.ini | 384 ----------------------------------------- pyproject.toml | 250 +++++++++++++++++++++++++++ 3 files changed, 251 insertions(+), 384 deletions(-) create mode 100644 changelog.d/11315.misc delete mode 100644 mypy.ini diff --git a/changelog.d/11315.misc b/changelog.d/11315.misc new file mode 100644 index 000000000000..e74573259fb2 --- /dev/null +++ b/changelog.d/11315.misc @@ -0,0 +1 @@ +Port mypy.ini into pyproject.toml. diff --git a/mypy.ini b/mypy.ini deleted file mode 100644 index f0af4ab28955..000000000000 --- a/mypy.ini +++ /dev/null @@ -1,384 +0,0 @@ -[mypy] -namespace_packages = True -plugins = mypy_zope:plugin, scripts-dev/mypy_synapse_plugin.py -follow_imports = normal -check_untyped_defs = True -show_error_codes = True -show_traceback = True -mypy_path = stubs -warn_unreachable = True -local_partial_types = True -no_implicit_optional = True - -files = - scripts-dev/sign_json, - setup.py, - synapse/, - tests/ - -# Note: Better exclusion syntax coming in mypy > 0.910 -# https://github.com/python/mypy/pull/11329 -# -# For now, set the (?x) flag enable "verbose" regexes -# https://docs.python.org/3/library/re.html#re.X -exclude = (?x) - ^( - |synapse/storage/databases/__init__.py - |synapse/storage/databases/main/__init__.py - |synapse/storage/databases/main/account_data.py - |synapse/storage/databases/main/cache.py - |synapse/storage/databases/main/devices.py - |synapse/storage/databases/main/directory.py - |synapse/storage/databases/main/e2e_room_keys.py - |synapse/storage/databases/main/end_to_end_keys.py - |synapse/storage/databases/main/event_federation.py - |synapse/storage/databases/main/event_push_actions.py - |synapse/storage/databases/main/events_bg_updates.py - |synapse/storage/databases/main/events_forward_extremities.py - |synapse/storage/databases/main/events_worker.py - |synapse/storage/databases/main/group_server.py - |synapse/storage/databases/main/media_repository.py - |synapse/storage/databases/main/metrics.py - |synapse/storage/databases/main/monthly_active_users.py - |synapse/storage/databases/main/presence.py - |synapse/storage/databases/main/profile.py - |synapse/storage/databases/main/purge_events.py - |synapse/storage/databases/main/push_rule.py - |synapse/storage/databases/main/receipts.py - |synapse/storage/databases/main/room.py - |synapse/storage/databases/main/roommember.py - |synapse/storage/databases/main/search.py - |synapse/storage/databases/main/state.py - |synapse/storage/databases/main/state_deltas.py - |synapse/storage/databases/main/stats.py - |synapse/storage/databases/main/transactions.py - |synapse/storage/databases/main/user_directory.py - |synapse/storage/schema/ - - |tests/api/test_auth.py - |tests/api/test_ratelimiting.py - |tests/app/test_openid_listener.py - |tests/appservice/test_scheduler.py - |tests/config/test_cache.py - |tests/config/test_tls.py - |tests/crypto/test_keyring.py - |tests/events/test_presence_router.py - |tests/events/test_utils.py - |tests/federation/test_federation_catch_up.py - |tests/federation/test_federation_sender.py - |tests/federation/test_federation_server.py - |tests/federation/transport/test_knocking.py - |tests/federation/transport/test_server.py - |tests/handlers/test_cas.py - |tests/handlers/test_directory.py - |tests/handlers/test_e2e_keys.py - |tests/handlers/test_federation.py - |tests/handlers/test_oidc.py - |tests/handlers/test_presence.py - |tests/handlers/test_profile.py - |tests/handlers/test_saml.py - |tests/handlers/test_typing.py - |tests/http/federation/test_matrix_federation_agent.py - |tests/http/federation/test_srv_resolver.py - |tests/http/test_fedclient.py - |tests/http/test_proxyagent.py - |tests/http/test_servlet.py - |tests/http/test_site.py - |tests/logging/__init__.py - |tests/logging/test_terse_json.py - |tests/module_api/test_api.py - |tests/push/test_email.py - |tests/push/test_http.py - |tests/push/test_presentable_names.py - |tests/push/test_push_rule_evaluator.py - |tests/rest/admin/test_admin.py - |tests/rest/admin/test_device.py - |tests/rest/admin/test_media.py - |tests/rest/admin/test_server_notice.py - |tests/rest/admin/test_user.py - |tests/rest/admin/test_username_available.py - |tests/rest/client/test_account.py - |tests/rest/client/test_events.py - |tests/rest/client/test_filter.py - |tests/rest/client/test_groups.py - |tests/rest/client/test_register.py - |tests/rest/client/test_report_event.py - |tests/rest/client/test_rooms.py - |tests/rest/client/test_third_party_rules.py - |tests/rest/client/test_transactions.py - |tests/rest/client/test_typing.py - |tests/rest/client/utils.py - |tests/rest/key/v2/test_remote_key_resource.py - |tests/rest/media/v1/test_base.py - |tests/rest/media/v1/test_media_storage.py - |tests/rest/media/v1/test_url_preview.py - |tests/scripts/test_new_matrix_user.py - |tests/server.py - |tests/server_notices/test_resource_limits_server_notices.py - |tests/state/test_v2.py - |tests/storage/test_account_data.py - |tests/storage/test_appservice.py - |tests/storage/test_background_update.py - |tests/storage/test_base.py - |tests/storage/test_client_ips.py - |tests/storage/test_database.py - |tests/storage/test_event_federation.py - |tests/storage/test_id_generators.py - |tests/storage/test_roommember.py - |tests/test_metrics.py - |tests/test_phone_home.py - |tests/test_server.py - |tests/test_state.py - |tests/test_terms_auth.py - |tests/test_visibility.py - |tests/unittest.py - |tests/util/caches/test_cached_call.py - |tests/util/caches/test_deferred_cache.py - |tests/util/caches/test_descriptors.py - |tests/util/caches/test_response_cache.py - |tests/util/caches/test_ttlcache.py - |tests/util/test_async_helpers.py - |tests/util/test_batching_queue.py - |tests/util/test_dict_cache.py - |tests/util/test_expiring_cache.py - |tests/util/test_file_consumer.py - |tests/util/test_linearizer.py - |tests/util/test_logcontext.py - |tests/util/test_lrucache.py - |tests/util/test_rwlock.py - |tests/util/test_wheel_timer.py - |tests/utils.py - )$ - -[mypy-synapse.api.*] -disallow_untyped_defs = True - -[mypy-synapse.app.*] -disallow_untyped_defs = True - -[mypy-synapse.crypto.*] -disallow_untyped_defs = True - -[mypy-synapse.events.*] -disallow_untyped_defs = True - -[mypy-synapse.handlers.*] -disallow_untyped_defs = True - -[mypy-synapse.push.*] -disallow_untyped_defs = True - -[mypy-synapse.rest.*] -disallow_untyped_defs = True - -[mypy-synapse.server_notices.*] -disallow_untyped_defs = True - -[mypy-synapse.state.*] -disallow_untyped_defs = True - -[mypy-synapse.storage.databases.main.client_ips] -disallow_untyped_defs = True - -[mypy-synapse.storage.databases.main.room_batch] -disallow_untyped_defs = True - -[mypy-synapse.storage.databases.main.user_erasure_store] -disallow_untyped_defs = True - -[mypy-synapse.storage.util.*] -disallow_untyped_defs = True - -[mypy-synapse.streams.*] -disallow_untyped_defs = True - -[mypy-synapse.util.batching_queue] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.cached_call] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.dictionary_cache] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.lrucache] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.response_cache] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.stream_change_cache] -disallow_untyped_defs = True - -[mypy-synapse.util.caches.ttl_cache] -disallow_untyped_defs = True - -[mypy-synapse.util.daemonize] -disallow_untyped_defs = True - -[mypy-synapse.util.file_consumer] -disallow_untyped_defs = True - -[mypy-synapse.util.frozenutils] -disallow_untyped_defs = True - -[mypy-synapse.util.hash] -disallow_untyped_defs = True - -[mypy-synapse.util.httpresourcetree] -disallow_untyped_defs = True - -[mypy-synapse.util.iterutils] -disallow_untyped_defs = True - -[mypy-synapse.util.linked_list] -disallow_untyped_defs = True - -[mypy-synapse.util.logcontext] -disallow_untyped_defs = True - -[mypy-synapse.util.logformatter] -disallow_untyped_defs = True - -[mypy-synapse.util.macaroons] -disallow_untyped_defs = True - -[mypy-synapse.util.manhole] -disallow_untyped_defs = True - -[mypy-synapse.util.module_loader] -disallow_untyped_defs = True - -[mypy-synapse.util.msisdn] -disallow_untyped_defs = True - -[mypy-synapse.util.patch_inline_callbacks] -disallow_untyped_defs = True - -[mypy-synapse.util.ratelimitutils] -disallow_untyped_defs = True - -[mypy-synapse.util.retryutils] -disallow_untyped_defs = True - -[mypy-synapse.util.rlimit] -disallow_untyped_defs = True - -[mypy-synapse.util.stringutils] -disallow_untyped_defs = True - -[mypy-synapse.util.templates] -disallow_untyped_defs = True - -[mypy-synapse.util.threepids] -disallow_untyped_defs = True - -[mypy-synapse.util.wheel_timer] -disallow_untyped_defs = True - -[mypy-synapse.util.versionstring] -disallow_untyped_defs = True - -[mypy-tests.handlers.test_user_directory] -disallow_untyped_defs = True - -[mypy-tests.storage.test_user_directory] -disallow_untyped_defs = True - -;; Dependencies without annotations -;; Before ignoring a module, check to see if type stubs are available. -;; The `typeshed` project maintains stubs here: -;; https://github.com/python/typeshed/tree/master/stubs -;; and for each package `foo` there's a corresponding `types-foo` package on PyPI, -;; which we can pull in as a dev dependency by adding to `setup.py`'s -;; `CONDITIONAL_REQUIREMENTS["mypy"]` list. - -[mypy-authlib.*] -ignore_missing_imports = True - -[mypy-bcrypt] -ignore_missing_imports = True - -[mypy-canonicaljson] -ignore_missing_imports = True - -[mypy-constantly] -ignore_missing_imports = True - -[mypy-daemonize] -ignore_missing_imports = True - -[mypy-h11] -ignore_missing_imports = True - -[mypy-hiredis] -ignore_missing_imports = True - -[mypy-hyperlink] -ignore_missing_imports = True - -[mypy-ijson.*] -ignore_missing_imports = True - -[mypy-jaeger_client.*] -ignore_missing_imports = True - -[mypy-josepy.*] -ignore_missing_imports = True - -[mypy-jwt.*] -ignore_missing_imports = True - -[mypy-lxml] -ignore_missing_imports = True - -[mypy-msgpack] -ignore_missing_imports = True - -[mypy-nacl.*] -ignore_missing_imports = True - -[mypy-netaddr] -ignore_missing_imports = True - -[mypy-opentracing] -ignore_missing_imports = True - -[mypy-parameterized.*] -ignore_missing_imports = True - -[mypy-phonenumbers.*] -ignore_missing_imports = True - -[mypy-prometheus_client.*] -ignore_missing_imports = True - -[mypy-pymacaroons.*] -ignore_missing_imports = True - -[mypy-pympler.*] -ignore_missing_imports = True - -[mypy-rust_python_jaeger_reporter.*] -ignore_missing_imports = True - -[mypy-saml2.*] -ignore_missing_imports = True - -[mypy-sentry_sdk] -ignore_missing_imports = True - -[mypy-service_identity.*] -ignore_missing_imports = True - -[mypy-signedjson.*] -ignore_missing_imports = True - -[mypy-treq.*] -ignore_missing_imports = True - -[mypy-twisted.*] -ignore_missing_imports = True - -[mypy-zope] -ignore_missing_imports = True diff --git a/pyproject.toml b/pyproject.toml index 8bca1fa4efd9..c908f7c303c8 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -54,3 +54,253 @@ exclude = ''' )/ ) ''' + +[tool.mypy] +namespace_packages = true +plugins = [ + "mypy_zope:plugin", + "scripts-dev/mypy_synapse_plugin.py", +] +follow_imports = "normal" +check_untyped_defs = true +show_error_codes = true +show_traceback = true +mypy_path = "stubs" +warn_unreachable = true +local_partial_types = true +no_implicit_optional = true + +files = [ + "scripts-dev/sign_json", + "setup.py", + "synapse/", + "tests/", +] + +# Note: Better exclusion syntax coming in mypy > 0.910 +# https://github.com/python/mypy/pull/11329 +# +# For now, set the (?x) flag enable "verbose" regexes +# https://docs.python.org/3/library/re.html#re.X + +exclude = """(?x) + ^( + |synapse/storage/databases/__init__.py + |synapse/storage/databases/main/__init__.py + |synapse/storage/databases/main/account_data.py + |synapse/storage/databases/main/cache.py + |synapse/storage/databases/main/devices.py + |synapse/storage/databases/main/directory.py + |synapse/storage/databases/main/e2e_room_keys.py + |synapse/storage/databases/main/end_to_end_keys.py + |synapse/storage/databases/main/event_federation.py + |synapse/storage/databases/main/event_push_actions.py + |synapse/storage/databases/main/events_bg_updates.py + |synapse/storage/databases/main/events_forward_extremities.py + |synapse/storage/databases/main/events_worker.py + |synapse/storage/databases/main/group_server.py + |synapse/storage/databases/main/media_repository.py + |synapse/storage/databases/main/metrics.py + |synapse/storage/databases/main/monthly_active_users.py + |synapse/storage/databases/main/presence.py + |synapse/storage/databases/main/profile.py + |synapse/storage/databases/main/purge_events.py + |synapse/storage/databases/main/push_rule.py + |synapse/storage/databases/main/receipts.py + |synapse/storage/databases/main/room.py + |synapse/storage/databases/main/roommember.py + |synapse/storage/databases/main/search.py + |synapse/storage/databases/main/state.py + |synapse/storage/databases/main/state_deltas.py + |synapse/storage/databases/main/stats.py + |synapse/storage/databases/main/transactions.py + |synapse/storage/databases/main/user_directory.py + |synapse/storage/schema/ + + |tests/api/test_auth.py + |tests/api/test_ratelimiting.py + |tests/app/test_openid_listener.py + |tests/appservice/test_scheduler.py + |tests/config/test_cache.py + |tests/config/test_tls.py + |tests/crypto/test_keyring.py + |tests/events/test_presence_router.py + |tests/events/test_utils.py + |tests/federation/test_federation_catch_up.py + |tests/federation/test_federation_sender.py + |tests/federation/test_federation_server.py + |tests/federation/transport/test_knocking.py + |tests/federation/transport/test_server.py + |tests/handlers/test_cas.py + |tests/handlers/test_directory.py + |tests/handlers/test_e2e_keys.py + |tests/handlers/test_federation.py + |tests/handlers/test_oidc.py + |tests/handlers/test_presence.py + |tests/handlers/test_profile.py + |tests/handlers/test_saml.py + |tests/handlers/test_typing.py + |tests/http/federation/test_matrix_federation_agent.py + |tests/http/federation/test_srv_resolver.py + |tests/http/test_fedclient.py + |tests/http/test_proxyagent.py + |tests/http/test_servlet.py + |tests/http/test_site.py + |tests/logging/__init__.py + |tests/logging/test_terse_json.py + |tests/module_api/test_api.py + |tests/push/test_email.py + |tests/push/test_http.py + |tests/push/test_presentable_names.py + |tests/push/test_push_rule_evaluator.py + |tests/rest/admin/test_admin.py + |tests/rest/admin/test_device.py + |tests/rest/admin/test_media.py + |tests/rest/admin/test_server_notice.py + |tests/rest/admin/test_user.py + |tests/rest/admin/test_username_available.py + |tests/rest/client/test_account.py + |tests/rest/client/test_events.py + |tests/rest/client/test_filter.py + |tests/rest/client/test_groups.py + |tests/rest/client/test_register.py + |tests/rest/client/test_report_event.py + |tests/rest/client/test_rooms.py + |tests/rest/client/test_third_party_rules.py + |tests/rest/client/test_transactions.py + |tests/rest/client/test_typing.py + |tests/rest/client/utils.py + |tests/rest/key/v2/test_remote_key_resource.py + |tests/rest/media/v1/test_base.py + |tests/rest/media/v1/test_media_storage.py + |tests/rest/media/v1/test_url_preview.py + |tests/scripts/test_new_matrix_user.py + |tests/server.py + |tests/server_notices/test_resource_limits_server_notices.py + |tests/state/test_v2.py + |tests/storage/test_account_data.py + |tests/storage/test_appservice.py + |tests/storage/test_background_update.py + |tests/storage/test_base.py + |tests/storage/test_client_ips.py + |tests/storage/test_database.py + |tests/storage/test_event_federation.py + |tests/storage/test_id_generators.py + |tests/storage/test_roommember.py + |tests/test_metrics.py + |tests/test_phone_home.py + |tests/test_server.py + |tests/test_state.py + |tests/test_terms_auth.py + |tests/test_visibility.py + |tests/unittest.py + |tests/util/caches/test_cached_call.py + |tests/util/caches/test_deferred_cache.py + |tests/util/caches/test_descriptors.py + |tests/util/caches/test_response_cache.py + |tests/util/caches/test_ttlcache.py + |tests/util/test_async_helpers.py + |tests/util/test_batching_queue.py + |tests/util/test_dict_cache.py + |tests/util/test_expiring_cache.py + |tests/util/test_file_consumer.py + |tests/util/test_linearizer.py + |tests/util/test_logcontext.py + |tests/util/test_lrucache.py + |tests/util/test_rwlock.py + |tests/util/test_wheel_timer.py + |tests/utils.py + )$ +""" + +[[tool.mypy.overrides]] +module = [ + "synapse.api.*", + "synapse.app.*", + "synapse.crypto.*", + "synapse.events.*", + "synapse.handlers.*", + "synapse.push.*", + "synapse.rest.*", + "synapse.server_notices.*", + "synapse.state.*", + "synapse.storage.databases.main.client_ips", + "synapse.storage.databases.main.room_batch", + "synapse.storage.databases.main.user_erasure_store", + "synapse.storage.util.*", + "synapse.streams.*", + "synapse.util.batching_queue", + "synapse.util.caches.cached_call", + "synapse.util.caches.dictionary_cache", + "synapse.util.caches.lrucache", + "synapse.util.caches.response_cache", + "synapse.util.caches.stream_change_cache", + "synapse.util.caches.ttl_cache", + "synapse.util.daemonize", + "synapse.util.file_consumer", + "synapse.util.frozenutils", + "synapse.util.hash", + "synapse.util.httpresourcetree", + "synapse.util.iterutils", + "synapse.util.linked_list", + "synapse.util.logcontext", + "synapse.util.logformatter", + "synapse.util.macaroons", + "synapse.util.manhole", + "synapse.util.module_loader", + "synapse.util.msisdn", + "synapse.util.patch_inline_callbacks", + "synapse.util.ratelimitutils", + "synapse.util.retryutils", + "synapse.util.rlimit", + "synapse.util.stringutils", + "synapse.util.templates", + "synapse.util.threepids", + "synapse.util.versionstring", + "synapse.util.wheel_timer", + "tests.handlers.test_user_directory", + "tests.storage.test_user_directory", +] +disallow_untyped_defs = true + +# Dependencies without annotations +# Before ignoring a module, check to see if type stubs are available. +# The `typeshed` project maintains stubs here: +# https://github.com/python/typeshed/tree/master/stubs +# and for each package `foo` there's a corresponding `types-foo` package on PyPI, +# which we can pull in as a dev dependency by adding to `setup.py`'s +# `CONDITIONAL_REQUIREMENTS["mypy"]` list. +[[tool.mypy.overrides]] +module = [ + "authlib.*", + "bcrypt", + "canonicaljson", + "constantly", + "daemonize", + "h11", + "hiredis", + "hyperlink", + "ijson.*", + "jaeger_client.*", + "josepy.*", + "jwt.*", + "lxml", + "msgpack", + "nacl.*", + "netaddr", + "opentracing", + "parameterized.*", + "phonenumbers.*", + "prometheus_client.*", + "pymacaroons.*", + "pympler.*", + "rust_python_jaeger_reporter.*", + "saml2.*", + "sentry_sdk", + "service_identity.*", + "signedjson.*", + "treq.*", + "twisted.*", + "zope", +] +ignore_missing_imports = true