Skip to content

python: Implement version-agnostic Protocol types#114

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

python: Implement version-agnostic Protocol types#114
bact wants to merge 19 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

github-actions Bot commented Jun 28, 2026

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>
bact added 3 commits July 1, 2026 21:18
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
@bact bact added this to the 1.2.0 milestone Jul 2, 2026
@bact bact requested review from JPEWdev and removed request for JPEWdev July 3, 2026 18:13
@bact bact marked this pull request as draft July 3, 2026 18:36
bact added 2 commits July 3, 2026 22:15
@bact

bact commented Jul 4, 2026

Copy link
Copy Markdown
Collaborator Author

This PR may need update after #124 got merged, as the protocols may need to have the new Ontology class too. (Possible to get that from auto-generation, but still need to add a test for confidence)

@bact bact marked this pull request as ready for review July 4, 2026 22:33
@bact

bact commented Jul 6, 2026

Copy link
Copy Markdown
Collaborator Author

#124 merged. Need to review this PR (may need few revision). Put to draft.

@bact bact marked this pull request as draft July 6, 2026 16:58
Follow the updated test.ttl

Signed-off-by: Arthit Suriyawongkul <arthit@gmail.com>
bact added 3 commits July 7, 2026 01:07
For cheap check of is_release, without the need to actually load the model

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 marked this pull request as ready for review July 9, 2026 00:00
@bact bact marked this pull request as draft July 9, 2026 00:00
@bact bact marked this pull request as ready for review July 9, 2026 11:30
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