GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Protocols by ilevkivskyi · Pull Request #417 · python/typing

Conversation

@ilevkivskyi

@ilevkivskyi ilevkivskyi commented Apr 26, 2017

Copy link
Copy Markdown
Member

Fixes #11

This is a runtime implementation of protocols as per draft PEP 544. (The mypy counterpart is python/mypy#3132)

The implementation is straightforward (mostly I just made current version to behave more similar to __subclasshook__ in collections.abc and added support for __annotations__ in Python 3.6).
Nevertheless, I add a lot of tests. Some of them may look trivial, but previous experience with generics shows that it is better to have them.

There is probably only one corner case worth noting. Consider this situation with for variable annotations:

@runtime
class Proto(Protocol):
    attr: int
class OtherProto(Protocol):
    attr: int
class BadConcrete:
    attr: int
class GoodConcrete:
    attr = 1

Then I propose the following subclass check results:

issubclass(GoodConcrete, Proto) == True
issubclass(BadConcrete, Proto) == False # This is a bit questionable, maybe return True?
issubclass(OtherProto, Proto) == True

The first one is probably obvious, the second one returns False to make this safe:

if issubclass(BadConcrete, Proto):
    BadConcrete().attr

The last one although safe since one cannot instantiate protocols, and this is probably what one would expect.

Attn: @JukkaL @ambv @gvanrossum

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

tebanep, wrmsr, ns-cweber, and lambdalisue reacted with thumbs up emoji ns-cweber and lambdalisue reacted with hooray emoji
All reactions
  • 👍 4 reactions
  • 🎉 2 reactions
ilevkivskyi and others added 16 commits April 20, 2017 00:40
@ilevkivskyi
Initial ideas for runtime implementation of protocols
de21b02
@ilevkivskyi
Uniform class and instance checks (same as collections.abc)
6a31a93
@ilevkivskyi
Fix lint
f107299
@ilevkivskyi
Simplify code using absence of non-protocol bases + some support for …
8e07b88
…PY 3.6
@ilevkivskyi
Better support for isinstance(); add some tests
e3db3de
@ilevkivskyi
Minor fixes
e580511
@ilevkivskyi
Add more tests; improve docstrings
d99d9ef
@ilevkivskyi
Add support for extending collections protocols (+ tests)
831e1a7
@ilevkivskyi
Fix behaviour in different veriosns
a932e9f
@ilevkivskyi
More compatibility fixes
3e34f7a
@ilevkivskyi
Fix typos and lint
8483810
@ilevkivskyi
Even more compatibility fixes
9e7b674
Ivan Levkivskyi
Reorganize code more logically (hopefully this will also reduce diff)
8ee5b71
@ilevkivskyi
Add tests for PY36 and @abstracmethod
4a0d335
@ilevkivskyi
Backport protocols to Python 2
8fc9a5e
@ilevkivskyi
Corner case for PEP 526 subprotocols
b6e526e
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Apr 26, 2017
@ilevkivskyi ilevkivskyi mentioned this pull request Apr 26, 2017
Convert selected ABCs to Protocols python/typeshed#1220
Merged
@ilevkivskyi ilevkivskyi mentioned this pull request May 9, 2017
Protocols python/mypy#3132
Merged
@ilevkivskyi

ilevkivskyi commented May 10, 2017 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

Something strange happened here (or maybe the comment was deleted), but here is an interesting note to make: because of how NamedTuple behaves at runtime (just making a collectios.namedtuple) it implements corresponding protocols already at the class level, for example:

@runtime
class P(Protocol):
    x: int
class N(NamedTuple):
    x: int

assert issubclass(N, P)  # OK
All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@Daenyth

Daenyth commented May 10, 2017

Copy link
Copy Markdown

I left a comment that I realized was completely incorrect so I deleted it

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi
Add test for named tuple with protocol
d3a5491
@ilevkivskyi ilevkivskyi mentioned this pull request May 16, 2017
NamedTupleInterface #431
Closed
ilevkivskyi added 2 commits July 28, 2017 21:22
@ilevkivskyi
Merge branch 'master' into protocols
eecef84
@ilevkivskyi
Fix broken merge
904c978
@ilevkivskyi

ilevkivskyi commented Jul 28, 2017

Copy link
Copy Markdown
Member Author

I will soon make another PR that will add (simplified) Protocol to typing_extensions. We cannot add
all the functionality while Protocol will be in typing_extensions since several classes (like SupportsInt etc.) subclass _Protocol in typing, ideally they should subclass the new Protocol, this is however quite minor thing.

I will keep this PR open (and update depending on the outcome of experiments with typing_extensions).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

ilevkivskyi added a commit to ilevkivskyi/typehinting that referenced this pull request Aug 19, 2017
@ilevkivskyi
Adapt Protocol and runtime from PR python#417
1ebd3f5
@ilevkivskyi ilevkivskyi mentioned this pull request Aug 19, 2017
Protocols in typing extensions #464
Merged
@gvanrossum

gvanrossum commented Sep 13, 2017

Copy link
Copy Markdown
Member

So IIUC we should first merge #464 and defer this one until the PEP has been accepted, right?

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi

ilevkivskyi commented Sep 13, 2017

Copy link
Copy Markdown
Member Author

So IIUC we should first merge #464 and defer this one until the PEP has been accepted, right?

Yes, also if there will be something we discover while playing with typing_extensions, I will update this PR accordingly (actually there are already few minor tweaks that I need to make here following the review of #464).

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi

ilevkivskyi commented Jun 18, 2019

Copy link
Copy Markdown
Member Author

This was superseded by #649

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ilevkivskyi ilevkivskyi closed this Jun 18, 2019
@ilevkivskyi
ilevkivskyi deleted the protocols branch June 18, 2019 00:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

CLA signed

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Protocols (a.k.a. structural subtyping)

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@ilevkivskyi @Daenyth @gvanrossum @the-knights-who-say-ni
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.