Skip to content

Attach docstrings to NewTypes at runtime#441

Open
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
newtype-docstrings
Open

Attach docstrings to NewTypes at runtime#441
Seth Fitzsimmons (mojodna) wants to merge 1 commit intodevfrom
newtype-docstrings

Conversation

@mojodna
Copy link
Collaborator

Summary

  • Convert bare triple-quoted strings after NewType assignments into explicit __doc__ assignments so docstrings are accessible at runtime
  • Python treats post-assignment docstrings as expression statements, never attaching them to the NewType object (__doc__ stays None)
  • Same pattern DocumentedEnum uses for enum member docs

Affected packages

  • overture-schema-system (primitives, strings, ref/id)
  • overture-schema-core (names, opening_hours)

Bare triple-quoted strings after NewType assignments are
expression statements that Python never attaches to the
NewType object, leaving __doc__ as None. Convert each to
an explicit __doc__ assignment so codegen and introspection
tools can read them at runtime.

Same pattern DocumentedEnum uses for enum member docs.
@vcschapp
Copy link
Collaborator

Can we talk about this? It breaks the documentation generator (pdoc IIRC), which has a special hack mode that recognizes docstrings coming after variables, but it doesn't support reading __doc__ on those values.

@mojodna
Copy link
Collaborator Author

Interesting... I had to do this to support Markdown/code generation because the docstrings that come after the NewType definitions weren't accessible programmatically (the opposite problem). It'd be interesting to see what proc's workaround is in case that's appropriate to incorporate in my work.

@RoelBollens-TomTom
Copy link
Collaborator

I also ran into the same issue where rather than explicit __doc__ field assignment I used the Field(description="...") instead. This still doesn't help with the pdoc documentation generation but wanted to mention it as there are models which currently document their fields in this way.

An other related issue I've come across has been with the Segment alias which is also not know anymore at runtime which I had solved using Field(title="...").

Copy link
Collaborator

@jenningsanderson Jennings Anderson (jenningsanderson) left a comment

Choose a reason for hiding this comment

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

👍 I think optimizing for markdown code generation here is okay for the moment, as this is what's required for our reference docs. We can/should revisit in the future for other documentation generators.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants