hypothesis / hypothesis/lms

Improve handling JSONSetting.format

Open
#7,047 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Python
Stars
53
Forks
16
Avg merge
14d 5h
Merged PRs (30d)
14

Description

The current enum has a mix of values (functions, types and one sentinel value) and it doesn't handle saving the value in a centralized way.

----

Some ideas from a PR:

The logic could also be a method of the enum. Something like:

```py
from enum import Enum, auto

class SettingType(Enum):
STRING = auto()
BOOLEAN = auto()

def tojson(self, value: str):
"""Parse a string to a JSON-serializable value appropriate for this setting type."""
match self:
case self.STRING:
return value
case self.BOOLEAN:
return value.lower() in ("true", "1")
case _:
raise ValueError("Unknown format")

bool_str = input("Enter a bool-ish string: ")
setting_type = Setting.BOOLEAN
print("Parsed value:", setting_type.tojson(bool_str))
```

_Originally posted by @robertknight in https://github.com/hypothesis/lms/pull/7041#discussion_r1991208259_

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.