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

What to do when future/stdlib/first_party categories aren't used? #48

Open
amyreese opened this issue Nov 18, 2020 · 3 comments
Open

What to do when future/stdlib/first_party categories aren't used? #48

amyreese opened this issue Nov 18, 2020 · 3 comments
Assignees
Labels
question Further information is requested

Comments

@amyreese
Copy link
Member

One thing that I've considered a couple times is the use of the hardcoded category values for stdlib and first party modules. Specifically for cases where the user potentially does the following in their project config:

[tool.usort]
categories = ["catchall", "numpy"]
default_category = "catchall"

The future, stdlib, and first-party detection, all hardcode categories that wouldn't be in the configured category list, and I'm not sure how that ends up working in the sorting system, or what the correct way to deal with this would be (other than more configuration options that I don't think we want). Maybe we need to do something like known[module] = CAT_FIRST_PARTY if CAT_FIRST_PARTY in config.categories else config.default_category. But maybe something that makes that easier rather than repeating that everywhere? 🤷

@amyreese amyreese added the question Further information is requested label Nov 18, 2020
@thatch
Copy link
Contributor

thatch commented Nov 19, 2020

If there was an elegant PR to support this, I'd be fine with fully customizable mapping. I think it's a lot of complexity that most people don't need, and for now would favor expanding this pattern of just complaining loudly to cover other validation issues:

https://github.com/facebookexperimental/usort/blob/5ba470b26d33142037692dc32e3029e08664a38a/usort/config.py#L151

It looks like we don't validate default_category in categories, nor all(cat in categories for cat in set(known.items()).

@amyreese amyreese self-assigned this Apr 3, 2023
@aentwist
Copy link

aentwist commented Oct 3, 2023

I think I just encountered this - basically would like a way for all imports to have one block, and have block separations be made manually only. I would like for blocks to be extremely intentional and vital to how the code runs, not just having them for stylistic reasons.

After reading the docs my userland self came to the conclusion to try variants of the following to get it,

[tool.usort]
default_category = "all"
categories = ["all"]
first_party_detection = false

but after trying several things, I guess it isn't supported.

The motivation for this is that in my experience, without it, naivete will take something like this

import datetime

import requests

and do this

import datetime

import requests
import zlib

which creates this

import datetime

import zlib

import requests

then all the sudden you have

import datetime

import zlib

import os
import io

from bar.bar import bar

import requests

import sqlalchemy

from . import foo

Does what I have described align with this issue?

@amyreese
Copy link
Member Author

amyreese commented Oct 3, 2023

The proliferation of blank lines is an issue that I plan to fix for version 1.1 — see #163 for that. This issue is specifically around handling non-standard configurations, because µsort hardcodes a couple category names in its handling of stdlib and relative imports, and then breaks if users have explicitly defined a set of categories that don't include those expected values.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants