beets icon indicating copy to clipboard operation
beets copied to clipboard

Add typings to the codebase

Open Serene-Arc opened this issue 1 year ago • 8 comments

Typings have been supported since python 3.5 and could be applied to the codebase. This would help readability a lot, help with IDE syntax, etc

Objective

Adding typings to the entirety of the codebase would be the goal although that's obviously a very long way in the future.

I'm happy to start and try and add these a module at a time.

Serene-Arc avatar Sep 13 '22 06:09 Serene-Arc

Certainly no objection! One thing I'm a little mystified by in this department is how to prioritize. We probably don't want to do it all at once, so we'd need to pick a couple modules or classes to start with. Any thoughts about how to choose those?

sampsyo avatar Sep 14 '22 00:09 sampsyo

@sampsyo There's really no single best approach imo. Perhaps for git reasons, changing the least used parts would be easiest first and less likely to impact other people's PRs and so on. Otherwise maybe doing similar ones in one fell swoop? Like doing all of the plugins (or most of them) at once.

Another approach would be starting with the core-most modules and then propogating outwards.

Serene-Arc avatar Sep 14 '22 00:09 Serene-Arc

I would speculate that it might be easiest to start adding types from the bottom up, in order to be able to know the types of called functions when trying to infer types for higher-level code.

It would also be great to add mypy to our CI to avoid adding incorrect annotations.

wisp3rwind avatar Sep 14 '22 20:09 wisp3rwind

Very true. Perhaps starting with dbcore would be a useful way in? It's kind of at the bottom of everything, touches all other modules (at least indirectly), and is meant to be abstract enough that the types won't be too crazy.

sampsyo avatar Sep 14 '22 23:09 sampsyo

That's probably a good idea, I can start with that. There is a pitfall that I and everyone else who attempts this has to be aware of though. The typings system as a whole was introduced in 3.5, it was expanded in 3.9 with additional types and annotations. These expanded types can't be used and will cause errors. So if beets is going to maintain compatibility with anything prior to 3.9, the collections typings especially will have to use the older versions.

Serene-Arc avatar Sep 15 '22 01:09 Serene-Arc

So I've started typing dbcore and I'll have that up in a PR soon for people to view. I still need to go and crossreference things and try and reconcile the typings, but I thought I should say: there are some places where the typings show that something is strange and possibly wrong. Do you want me to part or comment on these? Things like a list being passed when a dictionary is expected.

Serene-Arc avatar Sep 16 '22 04:09 Serene-Arc

Sure! Yeah, it's definitely possible that something's wrong, or that the typing is just kind of subtle and will require some thought to annotate correctly.

Maybe a good way to do it would be to open a WIP pull request with your current annotations, and then we can discuss in comment threads what to do for tricky/incorrect parts?

sampsyo avatar Sep 16 '22 15:09 sampsyo

Great, will do

Serene-Arc avatar Sep 16 '22 23:09 Serene-Arc