beetbox / beets

music library manager and MusicBrainz tagger
http://beets.io/
MIT License
12.57k stars 1.8k forks source link

Use typing_extensions only when needed #5331

Closed qarkai closed 1 week ago

qarkai commented 1 week ago

Description

Use typing_extensions only for Python <= 3.10.

Self was added in Python 3.11. TypeAlias was added in Python 3.10.

To Do

bal-e commented 1 week ago

Since type annotations are only used by mypy, maybe we should make these statements conditional on typing.TYPE_CHECKING as well? In any case, I think an explicit if statement (e.g. on the running Python version) would be preferable over catching an ImportError.

(Disclaimer: I'm not a maintainer, I'm just providing some feedback.)

qarkai commented 1 week ago

Ruff outputs runtime-import-in-type-checking-block (TCH004) warning if from typing_extensions import is in type-checking block. Not sure why.

bal-e commented 1 week ago

I suspect Ruff special-cases imports in type-checking blocks, only allowing things from modules it knows about. It definitely looks like they support some imports in type-checking blocks: it's mentioned here for example.

Serene-Arc commented 1 week ago

Hi! Thanks for the PR. What is the benefit of doing this rather than what we already have? Python 3.8 is currently our minimum supported version.

qarkai commented 1 week ago

Hi! Thanks for the PR. What is the benefit of doing this rather than what we already have? Python 3.8 is currently our minimum supported version.

Use built-in module in modern Python. Reduce dependencies, make life easier for downstream maintainers. Simplify upgrading to newer Python versions in the future, i.e. when minimum supported version is Python 3.11 typing_extensions could be removed completely.

Serene-Arc commented 1 week ago

Those benefits would only happen when we have Python 3.10 as the minimally supported version version though, wouldn't it? There'd be no difference until we reach that point, since it will still be on the dependency list regardless.

qarkai commented 1 week ago

Nope, if distro has Python >= 3.11 maintainer would be able to drop typing_extensions dependency for the next beets release with this patch even if the minimal supported version is still 3.8. It's in typing_extensions = { version = "*", python = "<=3.10" } line.