From cf3f76f817df1a500be1a6cc7a3a4b38be507861 Mon Sep 17 00:00:00 2001 From: rallyemax Date: Tue, 21 Sep 2021 16:27:41 -0600 Subject: [PATCH] Fix order of warnings filters The `warnings.filterwarnings` method inserts entries at the front of the filter list, and entries closer to the front of the list override entries later in the list, if both match a particular warning. (See https://docs.python.org/3/library/warnings.html) The intended behavior is for all `RemovedIn20Warning` warnings to log and continue, except that specific warnings that match the defined regular expressions should be raised as exceptions. To accomplish this intent, the general filter must be inserted before the regular expression special cases. The original order would work if `warnings.filterwarnings` for the default case were invoked with `append=True`, but the intent of the documentation is for this set of filters to be determinative (i.e. to override any conflicting pre-existing filters that may exist), so `append=True` should not be used. --- doc/build/changelog/migration_20.rst | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/doc/build/changelog/migration_20.rst b/doc/build/changelog/migration_20.rst index a5d90839b4..79e198d09c 100644 --- a/doc/build/changelog/migration_20.rst +++ b/doc/build/changelog/migration_20.rst @@ -260,7 +260,12 @@ the SQLAlchemy project itself, the approach taken is as follows: import warnings from sqlalchemy import exc - + + # for warnings not included in regex-based filter below, just log + warnings.filterwarnings( + "always", category=exc.RemovedIn20Warning + ) + # for warnings related to execute() / scalar(), raise for msg in [ r"The (?:Executable|Engine)\.(?:execute|scalar)\(\) function", @@ -276,11 +281,6 @@ the SQLAlchemy project itself, the approach taken is as follows: "error", message=msg, category=exc.RemovedIn20Warning, ) - # for all other warnings, just log - warnings.filterwarnings( - "always", category=exc.RemovedIn20Warning - ) - 3. As each sub-category of warnings are resolved in the application, new warnings that are caught by the "always" filter can be added to the list of "errors" to be resolved. -- 2.47.2