-
Notifications
You must be signed in to change notification settings - Fork 516
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
Syntax errors in README examples #219
Comments
Not sure what's going on here, maybe its a jsonschema package version mismatch? I'm on |
Thank you for opening an issue! It looks like a Python version issue. Make sure to install Python >= 3.10. Let me know if examples still fail. |
Hi, I noticed this too, but I would say rewriting the code might be better than forcing people to install 3.10. But I don't know how much work this would be. #234
def match_step_to_regex(step):
"""Translate an element of a JSON schema to a regex that defines its content.
Parameters
----------
step:
A string that represents the schema's structure, or a dictionnary
that represents a field in the schema.
Returns
-------
A string that represents a regular expression that defines the value of the
schedule's step.
"""
if isinstance(step, str):
return step
if isinstance(step, dict) and "enum" in step:
choices = step["enum"]
if step.get("type") == "string":
choices = [f'"{choice}"' for choice in choices]
else:
choices = [str(choice) for choice in choices]
return f"({'|'.join(choices)})"
if isinstance(step, dict) and "type" in step and step["type"] == "array" and "items" in step:
item_regexes = [match_step_to_regex(item) for item in step["items"]]
return rf"\[({','.join(item_regexes)})(,({','.join(item_regexes)}))*\]"
if isinstance(step, dict) and "type" in step and step["type"] == "object":
object_regexes = [match_step_to_regex(value) for value in step.values()]
return ''.join(object_regexes)
if isinstance(step, dict) and "type" in step and step["type"] == "string":
if "maxLength" in step:
return f'".{{,{step["maxLength"]}}}"'
elif "minLength" in step:
return f'".{{{step["minLength"]},}}"'
if isinstance(step, dict) and "type" in step:
field_type = step["type"]
return type_to_regex[field_type]
if isinstance(step, dict) and "anyOf" in step:
choices = step["anyOf"]
regexes = [match_step_to_regex(choice) for choice in choices]
return rf"({'|'.join(regexes)})"
raise NotImplementedError(f"Unsupported step: {step}") and a type hint def get_schema_pydantic(model: "type[BaseModel]"): |
I can create a PR for this if you allow me to work on it. |
Thank you for offering to help! Python 3.10 introduced structural pattern matching, which helps keep parts of the codebase sane, specifically JSON as you pointed out. This only supports a subset of the JSON schema, and as we add field constraints (#215) it will quickly become unmanageable and hard to understand. I would prefer not to change this requirement at this time. Nevertheless I'm open to arguments. |
I think the README might be outdated with the new 0.0.8 release
The text was updated successfully, but these errors were encountered: