-
Notifications
You must be signed in to change notification settings - Fork 1.8k
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
Fix types in beets/util/__init__.py
#5215
Conversation
Thank you for the PR! The changelog has not been updated, so here is a friendly reminder to check if you need to add an entry. |
a61d6c2
to
0109ca1
Compare
49dbfef
to
7154331
Compare
Nice! I'll have a look at this during the weekend. (In fact, I've also been working on this myself, let's see whether we came to the same conclusions.) |
I'm intrigued! |
Do we still support Python 3.7 @Serene-Arc @wisp3rwind? |
7154331
to
e807465
Compare
According to |
I'd be more than happy to drop it immediately then! I attempted to use |
Ok, than maybe let's not do it for this PR (it's not really required I suppose), but open a new issue to give people a chance to raise complaints about dropping Python 3.7. |
So, before I start looking at this in depth: Would you mind splitting into 3 PRs, namely:
(Or at least separate out the latter.) That would help a lot with reviewing, because the first two are pretty safe changes since they're not really changing any code. We should be able to merge them quickly. The latter might warrant more scrutiny. |
beets/util/__init__.py
Outdated
@@ -16,20 +16,20 @@ | |||
|
|||
import errno | |||
import fnmatch | |||
import functools |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is still required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup I accidentally removed it when I reversed my cached_property
import, assuming that nothing else needs this import.
Mypy was not happy here because `_legalize_stage` function implementation concatenates `path` and `extension` parameters, implying that their types need to match. You can see that initially `path` parameter was defined as a `str` while `extension` was `bytes`. In reality, depending on the `fragment` parameter value, `extension` was sometimes provided as a `str` and sometimes as `bytes`. The same parameter decided whether `path` gets converted into `bytes` within `_legalize_stage` implementation. No surprise that mypy was confused here. `_legalize_stage` is only used within `Item.destination` method implementation which accepts where `fragment` is defined. I determined that the `fragment` parameter controls the form of the output path: - fragment=False returned path absolute path *as bytes* (default) - fragment=True returned path relative to the library directory as *str*. Given the above, this commit 1. Renames `fragment` parameter to `relative_to_libdir` for clarity 2. Makes `Item.destination` to return the same type in both cases. I picked `bytes` since that's the type that majority of the code using this method expects. I converted the output path to str for the code that has been expecting a string here. 3. Decouples `_legalize_stage` and `_legalize_path` implementations from the `relative_to_libdir`. The logic now uses `str` type only.
7a25676
to
e53c7c7
Compare
Of course! |
@snejus could you also open another PR with the mypy config changes? Thanks! |
This PR is Part 1 of the work #5215 that fixes typing issues in `beets.util.__init__` module. It addresses simple-to-fix / most of the issues in this module.
Description
I noticed that two thirds of all typing issues in the codebase came from
beets.util.__init__
module, so I went ahead and addressed them.Before
After
Context for the
_legalize_path
/_legalize_stage
updateMypy was not happy here because
_legalize_stage
function implementation concatenatespath
andextension
parameters, implying that their types need to match.You can see that initially
path
parameter was defined as astr
whileextension
wasbytes
.In reality, depending on the
fragment
parameter value,extension
was sometimes provided as astr
and sometimes asbytes
. The same parameter decided whetherpath
gets converted intobytes
within_legalize_stage
implementation. No surprise that mypy was confused here._legalize_stage
is only used withinItem.destination
method implementation which is wherefragment
is defined. I determined that thefragment
parameter controls the form of the output path:fragment=False
returned absolute path as bytes (default)fragment=True
returned path relative to the library directory as str.Given the above, the change
Renames
fragment
parameter torelative_to_libdir
for clarityMakes
Item.destination
to return the same type in both cases. I pickedbytes
since that's the type that majority of the code using this method expects.I converted the output path to
str
for the code that has been expecting a string there.Decouples
_legalize_stage
and_legalize_path
implementations from therelative_to_libdir
. The logic now usesstr
type only.