Skip to content

Add py.typed marker file for PEP 561 compatibility #2484

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

Merged
merged 1 commit into from
May 17, 2025

Conversation

mohamadghaffari
Copy link
Contributor

This file signals to type checkers that the library provides inline type hints and should be type-checked, improving integration with tools like Mypy.

Description

This pull request introduces a py.typed marker file to the telebot package, by PEP 561.

Changes:

  • Added an empty telebot/py.typed file.

Purpose & Benefits:

  • PEP 561 Compliance: Signals to type checkers (like Mypy, Pyright, Pytype) that pyTelegramBotAPI ships with inline type information and is intended to be type-checked.
  • Improved Type Checking Experience: Allows type checkers to discover and utilize the library's existing type hints correctly. This resolves issues for users who have strict type checking configurations enabled (e.g., Mypy's disallow_any_unimported or check_untyped_defs), as they will no longer incorrectly flag telebot imports as untyped.
  • Enhanced Ecosystem Integration: Promotes better integration with the broader Python static typing ecosystem.

The existing Hatch build configuration ([tool.hatch.build.targets.wheel] include = ["telebot/*"]) in pyproject.toml ensures this new py.typed file will be included in the distributed wheel.

Describe your tests

Local Mypy Verification:
* Confirmed that without telebot/py.typed in my local pyTelegramBotAPI installation, Mypy version [1.15.0] (with disallow_any_unimported = true in pyproject.toml) reported errors for telebot imports (e.g., error: Type of variable becomes "Any | None" due to an unfollowed import [no-any-unimported]).
* Manually added the telebot/py.typed file to my local installation of pyTelegramBotAPI.
* Re-ran Mypy, and the previously mentioned errors related to telebot were resolved, indicating Mypy now correctly recognizes the library as typed.

Python version: [3.11]

OS: [macOS Sonoma]

Checklist:

  • I added/edited example on new feature/change (if exists)
    • N/A - This change improves type checking integration and doesn't introduce new library features requiring examples.
  • My changes won't break backward compatibility
  • I made changes both for sync and async
    • N/A - This change affects packaging and type information for the entire library, not specific sync/async code paths.

Additional Observation (Optional - For Maintainer Awareness):
During testing, I also ran mypy telebot/ directly on the library's source code after adding py.typed. This revealed several internal type consistency errors reported by Mypy. While this PR solely focuses on adding the py.typed marker to enable proper type checking by consumers (which it successfully does), these internal findings might interest the maintainers for future type hinting refinement within the library itself. This PR does not attempt to address those internal errors.

This file signals to type checkers that the library provides
inline type hints and should be type-checked, improving
integration with tools like Mypy.
@Badiboy
Copy link
Collaborator

Badiboy commented May 16, 2025

Signals to type checkers (like Mypy, Pyright, Pytype) that pyTelegramBotAPI ships with inline type information and is intended to be type-checked.

Not everything in pyTelegramBotAPI is typed. Will it hurt any cases?

@Badiboy
Copy link
Collaborator

Badiboy commented May 16, 2025

@coder2020official - I not actively use such type checkers, but I don't think it will make any bad.

@Badiboy Badiboy requested a review from Copilot May 16, 2025 17:49
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 1 out of 1 changed files in this pull request and generated no comments.

@Badiboy Badiboy requested a review from coder2020official May 16, 2025 17:49
@mohamadghaffari
Copy link
Contributor Author

Signals to type checkers (like Mypy, Pyright, Pytype) that pyTelegramBotAPI ships with inline type information and is intended to be type-checked.

Not everything in pyTelegramBotAPI is typed. Will it hurt any cases?

No, It just makes the typed parts of your library work better with type-checking tools, without negatively impacting the untyped parts for users.

@coder2020official
Copy link
Collaborator

Shouldn't we change pyproject.toml for that to work?

@mohamadghaffari
Copy link
Contributor Author

Shouldn't we change pyproject.toml for that to work?

You already included all telebot folder inside pyproject.toml. So, that is not required.
include = ["telebot/*"]

@coder2020official
Copy link
Collaborator

Well, I am not sure overall
I think we will need to leave this PR for now? I might need some more time to read and see how specifically it will affect the library.

@mohamadghaffari
Copy link
Contributor Author

Well, I am not sure overall I think we will need to leave this PR for now? I might need some more time to read and see how specifically it will affect the library.

Ok. You can check here: https://peps.python.org/pep-0561/ . It's also possible to be added by modifying the setup script like:

setup(
    ...,
    package_data = {
        'foopkg': ['py.typed'],
    },
    ...,
    )

But just adding the file worked for me as well.

@Badiboy
Copy link
Collaborator

Badiboy commented May 17, 2025

Well, I am not sure overall I think we will need to leave this PR for now? I might need some more time to read and see how specifically it will affect the library.

Everything you left "for read" is left forever. I propose to apply ))

@coder2020official
Copy link
Collaborator

Well if you propose applying, then do

@Badiboy Badiboy merged commit fefe38b into eternnoir:master May 17, 2025
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants