Skip to content

Commit

Permalink
docs: Update models to avoid including lazy defaults in the rendered …
Browse files Browse the repository at this point in the history
…man pages

Lazy defaults may return different results under different circumstances
and we always want man pages rendered consistently.
  • Loading branch information
dmach committed Dec 1, 2023
1 parent 00331e5 commit 608fc76
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions osc/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,10 @@ def get_default(name, field):
if field.default is None:
return None

if field.default_is_lazy:

Check warning on line 1327 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L1327

Added line #L1327 was not covered by tests
# lazy default may return different results under different circumstances -> return nothing
return None

Check warning on line 1329 in osc/conf.py

View check run for this annotation

Codecov / codecov/patch

osc/conf.py#L1329

Added line #L1329 was not covered by tests

ini_type = field.extra.get("ini_type", None)
if ini_type:
return None
Expand Down
3 changes: 3 additions & 0 deletions osc/util/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ def __init__(
# model sets it to None if it equals to NotSet (for better usability)
self.default = default

# a flag indicating, whether the default is a callable with lazy evalution
self.default_is_lazy = callable(self.default)

# whether the field was set
self.is_set = False

Expand Down
13 changes: 13 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,19 @@ class TestModel(BaseModel):

self.assertRaises(TypeError, TestModel)

def test_lazy_default(self):
class TestModel(BaseModel):
field: List[str] = Field(default=lambda: ["string"])

m = TestModel()
self.assertEqual(m.field, ["string"])

def test_lazy_default_invalid_type(self):
class TestModel(BaseModel):
field: List[str] = Field(default=lambda: None)

self.assertRaises(TypeError, TestModel)

def test_is_set(self):
class TestModel(BaseModel):
field: Optional[str] = Field()
Expand Down

0 comments on commit 608fc76

Please sign in to comment.