Skip to content

python: Implement version-agnostic Protocol types#114

Open
bact wants to merge 9 commits into
JPEWdev:mainfrom
bact:python-protocol
Open

python: Implement version-agnostic Protocol types#114
bact wants to merge 9 commits into
JPEWdev:mainfrom
bact:python-protocol

Conversation

@bact

@bact bact commented Jun 28, 2026

Copy link
Copy Markdown
Collaborator

This PR adds src/shacl2code/lang/templates/python/protocols.py.j2 template to generates Protocol structure types for classes in the generated bindings.

Given two versions of a model (v1 and v2; v2 is backward compatible with v1) to shacl2code to generate Python bindings (one at a time), it will generate two bindings that look similar but v1.SomeClass and v2.SomeClass are considered distinct. They are not sharing a common parent type.

This makes it difficult for a downstream user who wants to write a function like:

def func(x: SomeClass) -> SomeClass: ...

Because if it defines SomeClass as v1.SomeClass, it will be incompatible with v2.SomeClass (and vice versa).

This PR provides the generation of protocols.SomeClass which is a structural type that accepts any class with the same shape (has similar members), so v1.SomeClass and v2.SomeClass can be both accepted, even they are unrelated.

This function will work across all versions of the model (provided that they are keeping backward compatibility to previous versions -- not removing any members in newer versions):

def func(x: protocols.SomeClass) -> protocols.SomeClass: ...

bact added 3 commits June 27, 2026 22:36
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact bact added the enhancement New feature or request label Jun 28, 2026
@bact bact marked this pull request as draft June 28, 2026 03:30
@github-actions

Copy link
Copy Markdown

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  src/shacl2code/lang
  python.py
Project Total  

This report was generated by python-coverage-comment-action

bact added 6 commits June 28, 2026 09:47
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Follows --include-main pattern

Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant