Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow configuration to be defined on a file-scoped basis #7696

Open
Czaki opened this issue Sep 28, 2023 · 8 comments
Open

Allow configuration to be defined on a file-scoped basis #7696

Czaki opened this issue Sep 28, 2023 · 8 comments
Labels
configuration Related to settings and configuration

Comments

@Czaki
Copy link
Contributor

Czaki commented Sep 28, 2023

It will be nice to allow define banned-api rules per file pattern.

In general, there are API that may be useful in tests but should not be used in the library itself:
For example, warnings.catch_Warnings because of python/cpython#73858.

On the other hand, I would like to block using numpy.random in tests but allow in the library and/or examples (as it leads to random test failure).

If you think it is useful, I could try to contribute (learning rust).

@MichaReiser
Copy link
Member

That makes sense. Have you tried using a hierarchical configuration to achieve your goal? The way this works is that you create a new ruff.toml in the test directory that extends your default configuration. The test-specific configuration can override individual settings.

@Czaki
Copy link
Contributor Author

Czaki commented Sep 28, 2023

Unfortuantelly in this project (napari), test are not stored in a separate directory but in _test modules. And I'm only one of the maintainers.
(and this simplifies searching for a test of a given function).

@charliermarsh
Copy link
Member

Really wishing we had ESLint-style file overrides, as in:

{
  "rules": {
    "quotes": ["error", "double"]
  },

  "overrides": [
    {
      "files": ["bin/*.js", "lib/*.js"],
      "excludedFiles": "*.test.js",
      "rules": {
        "quotes": ["error", "single"]
      }
    }
  ]
}

@charliermarsh charliermarsh added the configuration Related to settings and configuration label Sep 29, 2023
@Czaki
Copy link
Contributor Author

Czaki commented Sep 29, 2023

cibuildwheel project use toml tables to implement overwrite rules inside pyproject.toml https://github.com/pypa/cibuildwheel/
https://cibuildwheel.readthedocs.io/en/stable/options/#overrides

Maybe it could be an inspiration?

@MichaReiser
Copy link
Member

Really wishing we had ESLint-style file overrides, as in:

{
  "rules": {
    "quotes": ["error", "double"]
  },

  "overrides": [
    {
      "files": ["bin/*.js", "lib/*.js"],
      "excludedFiles": "*.test.js",
      "rules": {
        "quotes": ["error", "single"]
      }
    }
  ]
}

I've mixed feelings about overrides. It is powerful but can become confusing if you mix it with extend. E.g. what happens if you have:

{
   "rules": {
     "quotes": ["error", "double"]
   },
 
   "overrides": [
     {
       "files": ["bin/*.js", "lib/*.js"],
       "excludedFiles": "*.test.js",
       "rules": {
         "quotes": ["error", "single"]
       }
     }
   ]
}

and

{
	"extend": "config-above.json",
   "rules": {
     "quotes": ["error", "double"]
   }
}

What does now bind stronger, the parent configuration because it's in the overrides section or the extend? I think we would need to change our configuration more drastically to make "overrides" feel intuitive, similar to what eslint does with the new flat configuration

@charliermarsh charliermarsh changed the title Allow define banned-api per file Allow configuration to be defined on a file-scoped basis Oct 11, 2023
@charliermarsh
Copy link
Member

(Making this issue more general -- it would be nice to support something like ESLint's flat configuration, in which you can define configuration that only applies to a subset of files.)

@DetachHead
Copy link

i'm fine with hierarchical configuration, but the only problem is that i wish it would inherit the flake8-tidy-imports.banned-api options from parent directories:

# ./pyproject.toml
[tool.ruff.flake8-tidy-imports.banned-api]
"foo".msg = "banned project-wide"
# ./tests/ruff.toml
extend = "../pyproject.toml"

[flake8-tidy-imports.banned-api]
"bar".msg = "only banned in tests"
# tests/test_foo.py
import foo # no error, because the `banned-api` the `tests` directory doesn't inherit from the global one

@BT-rmartin
Copy link

In my case #7890 we want to configure isort on file-scope basis, and in our particular need the hierarchical solution is not feasible, as we want it for every init.py file. Is there any progress on this topic @charliermarsh ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
configuration Related to settings and configuration
Projects
None yet
Development

No branches or pull requests

5 participants