Skip to content

Enforce Optional[T] typing for None-defaulted Pydantic fields#3209

Open
jeffreyksmithjr wants to merge 1 commit intonebari-dev:mainfrom
jeffreyksmithjr:enforce-optional-none-fields
Open

Enforce Optional[T] typing for None-defaulted Pydantic fields#3209
jeffreyksmithjr wants to merge 1 commit intonebari-dev:mainfrom
jeffreyksmithjr:enforce-optional-none-fields

Conversation

@jeffreyksmithjr
Copy link
Copy Markdown

Reference Issues or PRs

See also: nebari-dev/nebari-docs#615 (companion docs PR)

What does this implement/fix?

Put a x in the boxes that apply

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds a feature)
  • Breaking change (fix or feature that would cause existing features not to work as expected)
  • Documentation Update
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no API changes)
  • Build related changes
  • Other (please describe):

Documentation

Testing

  • Did you test the pull request locally?
  • Did you add new tests?

How to test this PR?

Run the new enforcement test:

pytest tests/tests_unit/test_code_conventions.py -v

The test dynamically imports all modules under nebari and _nebari, collects
every Pydantic BaseModel subclass, and verifies that any field defaulting to
None includes NoneType in its resolved annotation. It handles Optional,
Union, T | None, and Annotated wrappers.

Any other comments?

Two existing violations were fixed:

  • GCPClusterInputVars.node_group_image_type in stages/infrastructure/__init__.py
  • Storage.type in stages/kubernetes_services/__init__.py

Both already had Optional imported in their respective files.

Fix two fields that default to None without including None in their
type annotation. Add a test that introspects all nebari Pydantic models
at runtime and fails if any field defaults to None without an explicit
Optional (or Union with None) in its annotation.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: New 🚦

Development

Successfully merging this pull request may close these issues.

1 participant