Skip to content

Support Python 3.15 feature: Lazy imports #5231

@harshil21

Description

@harshil21

What kind of feature are you missing? Where do you notice a shortcoming of PTB?

With the Python 3.15 beta out, we should test if our library can work with the new version without any problems.

One of the features we can instantly make use of is Lazy Imports. Currently, importing the telegram module takes about 0.25 seconds on my machine with Python 3.14.3.

Lazy imports should help cut that down by a little bit at least.

Describe the solution you'd like

  • Add 3.15 runners to the test suite
  • Migrate 3.14t runner to 3.15t runner
  • Use the __lazy_modules__ feature on our __init__ files so that the user doesn't have to import objects they won't be using.

Describe alternatives you've considered

Keep imports inside functions, but that is unpythonic and ugly

Additional context

Whenever we drop support for 3.14, we can also get rid of our TYPE_CHECKING blocks, as that import won't be evaluated at import time, which should also help reduce circular import errors too.

Metadata

Metadata

Assignees

Labels

Priority

Medium

Effort

Low

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions