Skip to content

How to satisfy Typing.Sequence using Structural typing #8287

Closed Answered by erictraut
omar-abdelgawad asked this question in Q&A
Discussion options

You must be logged in to vote

Pyright is a standards-compliant type checker for Python, which means it follows the rules spelled out in the Python type specification.

Sequence is a nominal type, not a structural type. In the Python type system, type compatibility rules for nominal types is based on static class hierarchies. Class B is a subtype of class A only if B derives from A. Dynamic registration of ABCs is not (and never will be) supported in the static type system.

In the Python type system, structural types are referred to as “protocols”. You can read about them in the Python type specification here and here. Examples of protocols includes Sized, Iterable, Container, and Reversible. You could create a protocol…

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@omar-abdelgawad
Comment options

@erictraut
Comment options

@erictraut
Comment options

Answer selected by omar-abdelgawad
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants