Skip to content

Generate version-agnostic Protocols for any SPDX 3.x#52

Draft
bact wants to merge 3 commits into
spdx:mainfrom
bact:shacl2code-protocol
Draft

Generate version-agnostic Protocols for any SPDX 3.x#52
bact wants to merge 3 commits into
spdx:mainfrom
bact:shacl2code-protocol

Conversation

@bact

@bact bact commented Jun 29, 2026

Copy link
Copy Markdown
Collaborator

Note

This PR requires SHACLObjectSetProtocol(Protocol), etc from JPEWdev/shacl2code#114 (shacl2code>1.1.0). It will failed the test until we got the Protocol support from the bindings that shacl2code generated (using new --include-protocols option).

This PR contains changes from #46 (Add SPDX 3.1), because we need SPDX 3.1 binding to test Protocols. Consider merging #46 before this one.

What this PR trying to do

Each minor version has its own generated types, so v3_0_1.Element and
v3_1.Element are technically distinct. The protocols submodule exposes
version-neutral protocol types so one function works with any 3.x version:

from typing import Optional
from spdx_python_model import protocols

def rename(e: protocols.Element, name: str) -> Optional[str]:
    e.name = name
    return e.name

Class hierarchy is preserved (protocols.Agent accepts Person, Tool, etc.
from any version). These protocols are for static typing only; construct objects
with a concrete version (v3_0_1.Person() or model.Person() from load()).

How it gets done

The gen/generate-protocols script combines spdx_python_model.bindings.protocols from each generated version of SPDX 3 model (from shacl2code) into to a single version-agnostic spdx_python_model.protocols that can be used across all version of SPDX 3 (provided that SPDX 3 keeps backward compatibility across minor versions).

Merge rules:

  • Earliest-wins per class. Each class is re-exported from the oldest version that introduced it. This gives the minimum property set, which every later version's concrete class is guaranteed to satisfy (since newer versions only add properties, never remove them). The minimum property set (= no more than this set) is required for structural type check.
  • New classes from later versions are included. A class introduced in 3.1 (e.g. hardware_Hardware) is re-exported from v3.1 and accepts objects from 3.1 onwards.

bact added 2 commits June 28, 2026 04:24
(This built on top of the bact:add-spdx-3-1 branch that adds SPDX 3.1 model loading)

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 29, 2026
@bact bact marked this pull request as draft June 29, 2026 14:53
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