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

Inheritance, Composition and Mixins #9

Open
simontaurus opened this issue Jul 3, 2024 · 0 comments
Open

Inheritance, Composition and Mixins #9

simontaurus opened this issue Jul 3, 2024 · 0 comments

Comments

@simontaurus
Copy link
Contributor

simontaurus commented Jul 3, 2024

See also https://linkml.io/linkml/schemas/inheritance.html#mixin-classes-and-slots and https://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem.
The diamond problem does not apply to JSON-SCHEMA since every schema included by allOf needs to be valid on the target document, meaning the following schema is invalid in any case:

allOf:
- title: B
  properties:
    x:
      title: xB
      type: integer
- title: C
  properties:
    x:
      title: xC
      type: string

This make sense also in case of beeing transferred to e.g. python dateclasses because otherwise casting from a more specific to a more general base class would not be possible.
The need to allow incompatible definitions applies actually only to annotations (like title, descriptions, etc) and class methods (out-of-scope for JSON-SCHEMA). In this case, the order of superclasses becomes important, e.g.

allOf:
- title: B
  properties:
    x:
      title: xB
      type: integer
      minimum: 1
- title: C
  properties:
    x:
      title: xC
      type: integer
      maximum: 10

will generate a web form with xB as label for x and a code generator will create

class Model(BaseModel):
    x: Optional[conint(ge=1, le=10)] = Field(None, title="xB")

Consistently,

class B:
    def echo(self):
        print("xB")
        
class C:
    def echo(self):
        print("xC")
        
class D(B,C):
    pass

D().echo()

will result in xB.

Other then the order with allOf there's no intrument in JSON-SCHEMA to indicate primary and secundary superschemas / classes. $ref could be regarded to have a different semantics as allOf: $ref but only a single value is allowed for $ref.

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

No branches or pull requests

1 participant