Thanks to visit codestin.com
Credit goes to github.com

Skip to content

stubgen: fixed handling of Protocol and added testcase #12129

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 3 commits into from
Mar 18, 2022

Conversation

citruz
Copy link
Contributor

@citruz citruz commented Feb 5, 2022

Description

This PR fixes #12072 by correctly handling Protocol definitions. Previously, the Protocol base class was removed when generating type stubs which causes problems with other packages that want to use type definition (because they see it as a regular class, not as a Protocol).

Test Plan

Added a testcase to the stubgen testset.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thank you!

[case testProtocol_semanal]
from typing import Protocol

class P(Protocol):
Copy link
Member

Choose a reason for hiding this comment

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

Let's also add a generic Protocol[T] class here 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

You mean like this?

T = TypeVar('T')
class PT(Protocol[T]):
    def f(self, x: T) -> T:
        ...

That results in:

  T = TypeVar('T')

  class PT(Protocol):
      def f(self, x: T) -> T: ...

So I guess that needs to be handled explicitly in my implementation?

Copy link
Member

Choose a reason for hiding this comment

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

Yes 🙂

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done ✅

@github-actions

This comment has been minimized.

Copy link
Member

@sobolevn sobolevn left a comment

Choose a reason for hiding this comment

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

Thank you! 👍

@sobolevn
Copy link
Member

sobolevn commented Feb 8, 2022

@srittau do you want to double check this? I've seen multiple your PRs to stubgen 🙂

@citruz
Copy link
Contributor Author

citruz commented Feb 20, 2022

Any updates on this? :)

@JelleZijlstra
Copy link
Member

If you fix the merge conflict I can review it and hopefully merge it.

@github-actions
Copy link
Contributor

According to mypy_primer, this change has no effect on the checked open source code. 🤖🎉

@JelleZijlstra JelleZijlstra merged commit 21d957a into python:master Mar 18, 2022
@citruz citruz deleted the fix-protocols branch March 18, 2022 06:50
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.

stubgen removes Protocol from base classes list
5 participants