Restore Blueprint support in CORS type signatures (#410) - #411
Merged
Conversation
The strict typing added in #409 (released in 6.0.4) narrowed the ``app`` parameter of ``CORS.__init__``/``init_app`` to ``Flask | None``. Flask-CORS supports applying CORS directly to a ``Blueprint`` at runtime (``get_app_kwarg_dict`` already guards for the missing ``config`` attribute, and the exception-handler wrapping is guarded by ``hasattr``), so passing a Blueprint still worked but failed ``mypy --strict`` for downstream users. - Widen the accepted type to ``Flask | Blueprint | None`` on ``CORS.__init__``, ``init_app``, and the internal ``merge_options`` / ``get_cors_options`` / ``get_app_kwarg_dict`` helpers. - Route the Flask-only ``make_response`` / ``handle_exception`` accesses through an ``Any`` alias, gated by the existing ``hasattr`` guard, so the union type checks cleanly. - Add runtime tests exercising CORS on a Blueprint via both the constructor and ``init_app`` (there was previously no Blueprint test coverage). - Add a type-checking regression test (``tests/typecheck``) included in the mypy ``files`` list so a future narrowing of the signature fails CI. - Type-check the ``examples`` directory under strict mypy as well, fixing the ``os.sys`` path hack and adding view return annotations.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #410.
The strict typing added in #409 (released in 6.0.4) narrowed the
appparameter ofCORS.__init__/init_apptoFlask | None. Flask-CORS supports applying CORS directly to aBlueprintat runtime —get_app_kwarg_dictalready guards for the missingconfigattribute, and the exception-handler wrapping is guarded byhasattr(app, "handle_exception")— so passing a Blueprint still worked, but it began failingmypy --strictfor downstream users:This realigns the type annotation with the behavior the runtime guards already implement.
Changes
Flask | Blueprint | NoneonCORS.__init__,init_app, and the internalmerge_options/get_cors_options/get_app_kwarg_dicthelpers.make_response/handle_exceptionaccesses through anAnyalias, gated by the existinghasattrguard, so the wider union type-checks cleanly (no behavior change — the block already only ran for full apps).init_app(there was previously no Blueprint test coverage).tests/typecheck/) included in the mypyfileslist, so a future narrowing of the signature fails CI. Verified: reverting the signature makes mypy fail on exactly that test.examples/directory under strict mypy as well, fixing the non-exportedos.syspath hack and adding view return annotations.Verification
mypy— clean (9 source files)pytest— 98 passed, 1 skippedruff— cleanFollow-up
6.0.4 isn't broken at runtime (it only fails strict typing for Blueprint users), so the suggested path is to release this as 6.0.5 and then yank 6.0.4 on PyPI with a reason pointing at 6.0.5 (yank keeps it installable for anyone already pinned, while steering new installs past it).
Generated by Claude Code