Skip to content
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

[composites] use typing.Sequence for children argument #436

Merged
merged 1 commit into from
Aug 31, 2024

Conversation

emdemir
Copy link

@emdemir emdemir commented Feb 23, 2024

The Python type system considers typing.List to be mutable, and thus invariant on its child type. This doesn't let us pass Behaviour subclasses to composites through the children parameter, forcing the use of composite.add_child(child) or the use of typing.cast. Since the children list is never mutated, it's better to use typing.Sequence here which is covariant and therefore typechecks when a list of Behaviour subclasses are passed in.

The Python type system considers `typing.List` to be mutable, and thus
invariant on its child type. This doesn't let us pass `Behaviour`
subclasses to composites through the `children` parameter, forcing the
use of `composite.add_child(child)` or the use of `typing.cast`. Since
the `children` list is never mutated, it's better to use
`typing.Sequence` here which is covariant and therefore typechecks when
a list of `Behaviour` subclasses are passed in.
Copy link
Collaborator

@sea-bass sea-bass left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me. This resource was helpful as a reference: https://blog.meadsteve.dev/programming/2023/09/09/typed-python-prefer-sequence-over-list/

@sea-bass sea-bass merged commit 07fdc76 into splintered-reality:devel Aug 31, 2024
3 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.

2 participants