PyCQA / isort

A Python utility / library to sort imports.
https://pycqa.github.io/isort/
MIT License
6.49k stars 580 forks source link

Using Regex to specify modules #1605

Open Abdur-rahmaanJ opened 3 years ago

Abdur-rahmaanJ commented 3 years ago

Is it possible to use regex like:

modules.*.*

timothycrosley commented 3 years ago

Do you have any more context? Do you mean for config options such as known_first_party? The following should work for that the same known_first_party=modules as the following * is implied

Abdur-rahmaanJ commented 3 years ago

I mean if i want to group every module.*.models together. Like module.fish.model, module.octopus.model, module.tortoise.model. A way to specify those

timothycrosley commented 3 years ago

That's an interesting concept! It's not currently supported, but I'm open to support simple patterns (probably not full regex, but simple * support like shown)

CarlosRDomin commented 3 years ago

I'd like to bring up a related use-case where regex (or at least some simple pattern support) could be very helpful.
Say your organization adds a prefix to any internal python modules, e.g. myprefix_*, and say that you're working on a module called myprefix_example. It's helpful to group all organization-related imports as known_org=["myprefix_*"], which works great except that it screws up the Project Imports since any code inside the folder myprefix_example is also assumed to be within the Organization Imports.

In the past (I think pre- isort 5) regex worked, so we just did:

known_org=["^myprefix(?!_example$)"]

With isort 5, that stopped working, but we can do:

known_first_party = ["myprefix_example", "myprefix_example.*"]
known_org=["myprefix_*"]

Besides not being very convenient to have to write the full name of your src folder for every repo, the main issue is that if you don't add the second item ("myprefix_example.*"), any absolute imports within your project would also be mis-detected as known_org instead of known_first_party.

Or perhaps just a way to always keep project imports as known_first_party would be enough?