You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "Taragolis (via GitHub)" <gi...@apache.org> on 2024/02/28 13:05:00 UTC

[PR] Enable more `flake8-pytest-style` (PT) rules [airflow]

Taragolis opened a new pull request, #37772:
URL: https://github.com/apache/airflow/pull/37772

   <!--
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at
   
      http://www.apache.org/licenses/LICENSE-2.0
   
    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
    -->
   
   <!--
   Thank you for contributing! Please make sure that your code changes
   are covered with tests. And in case of new features or big changes
   remember to adjust the documentation.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   Enable auto-fixable or partially fixable [flake8-pytest-style (PT) rules](https://docs.astral.sh/ruff/rules/#flake8-pytest-style-pt)
   
   ```console
   Run 'ruff' for extremely fast Python linting.............................Failed
   - hook id: ruff
   - exit code: 1
   - files were modified by this hook
   
   tests/cli/commands/test_dag_command.py:25:22: TID251 `unittest.TestCase` is banned: Use pytest compatible classes
   tests/cli/commands/test_dag_command.py:601:18: PT027 Use `pytest.raises` instead of unittest-style `assertRaises`
   tests/conftest.py:1115:31: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/decorators/test_bash.py:38:21: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/plugins/test_plugins_manager.py:168:35: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/hooks/test_eks.py:102:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/hooks/test_eks.py:136:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/hooks/test_eks.py:178:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/sensors/test_eks.py:71:21: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/sensors/test_eks.py:112:21: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/amazon/aws/sensors/test_eks.py:160:21: PT003 `scope='function'` is implied in `@pytest.fixture()`
   Found 58 errors (47 fixed, 11 remaining).
   No fixes available (9 hidden fixes can be enabled with the `--unsafe-fixes` option).
   tests/providers/cncf/kubernetes/operators/test_pod.py:75:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/docker/operators/test_docker.py:141:35: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/docker/test_exceptions.py:49:35: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/fab/auth_manager/test_security.py:157:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/fab/auth_manager/test_security.py:192:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/fab/auth_manager/test_security.py:204:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/fab/auth_manager/test_security.py:215:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/microsoft/azure/hooks/test_container_instance.py:35:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/openlineage/extractors/test_bash.py:46:31: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/providers/openlineage/extractors/test_python.py:61:31: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/test_utils/system_tests_class.py:70:35: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/ti_deps/deps/test_not_previously_skipped_dep.py:34:31: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/ti_deps/deps/test_runnable_exec_date_dep.py:34:31: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/utils/log/test_secrets_masker.py:451:21: PT003 `scope='function'` is implied in `@pytest.fixture()`
   tests/www/views/test_views_trigger_dag.py:40:17: PT003 `scope='function'` is implied in `@pytest.fixture()`
   Found 147 errors (132 fixed, 15 remaining).
   No fixes available (15 hidden fixes can be enabled with the `--unsafe-fixes` option).
   ```
   
   The only one which are not enabled is [`PT022`](https://docs.astral.sh/ruff/rules/pytest-useless-yield-fixture/)
   
   In some easy cases it produce expected result
   
   ```diff
   diff --git a/tests/cli/commands/test_info_command.py b/tests/cli/commands/test_info_command.py
   index 7203371484..44287bb0ec 100644
   --- a/tests/cli/commands/test_info_command.py
   +++ b/tests/cli/commands/test_info_command.py
   @@ -174,7 +174,7 @@ class TestAirflowInfo:
    
    @pytest.fixture
    def setup_parser():
   -    yield cli_parser.get_parser()
   +    return cli_parser.get_parser()
   ```
   
   In another it produces semantically correct replacement but personally I've found it ugly. So better to fix it manually by the separate PR and disable autofix for this rule
   
   ```diff
   diff --git a/tests/conftest.py b/tests/conftest.py
   index 1847e1e909..e41d03411b 100644
   --- a/tests/conftest.py
   +++ b/tests/conftest.py
   @@ -131,7 +131,7 @@ def reset_db():
        from airflow.utils import db
    
        db.resetdb()
   -    yield
   +    return
   ```
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable more `flake8-pytest-style` (PT) rules [airflow]

Posted by "Taragolis (via GitHub)" <gi...@apache.org>.
Taragolis commented on code in PR #37772:
URL: https://github.com/apache/airflow/pull/37772#discussion_r1505953212


##########
pyproject.toml:
##########
@@ -1497,10 +1505,16 @@ banned-module-level-imports = ["numpy", "pandas"]
 "flask.escape".msg = "Use markupsafe.escape instead. Deprecated in Flask 2.3, removed in Flask 3.0"
 "flask.Markup".msg = "Use markupsafe.Markup instead. Deprecated in Flask 2.3, removed in Flask 3.0"
 "flask.signals_available".msg = "Signals are always available. Deprecated in Flask 2.3, removed in Flask 3.0"
+# Some specific cases
+"unittest.TestCase".msg = "Use pytest compatible classes"
 
 [tool.ruff.lint.flake8-type-checking]
 exempt-modules = ["typing", "typing_extensions"]
 
+[tool.ruff.lint.flake8-pytest-style]
+mark-parentheses = false
+fixture-parentheses = false

Review Comment:
   `mark-parentheses`: if True (default for ruff) then `@pytest.mark.db_test()` is valid option for non parametrised mark otherwise `@pytest.mark.db_test`
   `fixture-parentheses`: if True (default for ruff) then use `@pytest.fixture()` is valid option for non parametrised fixture otherwise `@pytest.fixture`
   
   Particular this option generate less changes, so assumption here that it is correct selection for this rules
   
   |                                 | **mark-parentheses  =  `false`** | **mark-parentheses  =  `true`** |
   |---------------------------------|--------------------------------|-------------------------------|
   | **fixture-parentheses = `false`** | 179 errors in 74 files         | 990 errors in 267 files       |
   |  **fixture-parentheses = `true`** | 271 errors in 150 files        | 1082 errors in 311 files      |



##########
tests/cli/commands/test_dag_command.py:
##########
@@ -598,12 +598,12 @@ def test_cli_list_import_errors(self):
         dag_path = os.path.join(TEST_DAGS_FOLDER, "test_invalid_cron.py")
         args = self.parser.parse_args(["dags", "list", "--output", "yaml", "--subdir", dag_path])
         with contextlib.redirect_stdout(StringIO()) as temp_stdout:
-            with TestCase.assertRaises(TestCase(), SystemExit) as context:
+            with pytest.raises(SystemExit) as err_ctx:

Review Comment:
   I've baned import `unittest.TestCase`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable more `flake8-pytest-style` (PT) rules [airflow]

Posted by "Taragolis (via GitHub)" <gi...@apache.org>.
Taragolis merged PR #37772:
URL: https://github.com/apache/airflow/pull/37772


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable more `flake8-pytest-style` (PT) rules [airflow]

Posted by "Taragolis (via GitHub)" <gi...@apache.org>.
Taragolis commented on code in PR #37772:
URL: https://github.com/apache/airflow/pull/37772#discussion_r1505964094


##########
tests/cli/commands/test_dag_command.py:
##########


Review Comment:
   Manual changes in this module



##########
tests/providers/amazon/aws/sensors/test_eks.py:
##########


Review Comment:
   Manual changes in this module



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] Enable more `flake8-pytest-style` (PT) rules [airflow]

Posted by "Taragolis (via GitHub)" <gi...@apache.org>.
Taragolis closed pull request #37772: Enable more `flake8-pytest-style` (PT) rules
URL: https://github.com/apache/airflow/pull/37772


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org