Skip to content
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

fix(schema): add basic support of Pattern type in schema generation #1768

Merged
merged 1 commit into from Oct 8, 2020

Conversation

PrettyWood
Copy link
Member

@PrettyWood PrettyWood commented Jul 27, 2020

Change Summary

Since typing.Pattern is not a valid type, an error is raised when generating the schema.
This PR adds a basic support for it.

Related issue number

closes #1767
closes #1269

Checklist

  • Unit tests for the changes exist
  • Tests pass on CI and coverage remains at 100%
  • Documentation reflects the changes where applicable
  • changes/<pull request or issue id>-<github username>.md file added describing change
    (see changes/README.md for details)

@codecov
Copy link

codecov bot commented Jul 27, 2020

Codecov Report

Merging #1768 into master will not change coverage.
The diff coverage is 100.00%.

@@            Coverage Diff            @@
##            master     #1768   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files           21        21           
  Lines         3895      3895           
  Branches       783       783           
=========================================
  Hits          3895      3895           
Impacted Files Coverage Δ
pydantic/schema.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e985857...3df1556. Read the comment docs.

@Gatux
Copy link

Gatux commented Jul 28, 2020

Hi PrettyWood,

Thanks for the PR.

I think it would be better if we provide a format, for exemple: 'properties': {'filter': {'type': 'string', 'title': 'Filter', 'format': 'regex'}}

This is because with a model like this:

class Model(BaseModel):
    filter: Pattern = '^regex$'

the property filtermust be a valid regex (and not a string validated by a regex)

It is not the same as filter: constr(regex='^regex$')where the schema must provide the property "pattern": "^regex$") asfilterwill be validated against the regex ^regex$.

What do you think ?

Also we may need to add this new type in the doc: https://pydantic-docs.helpmanual.io/usage/schema/#json-schema-types

[edit] typo

@PrettyWood
Copy link
Member Author

PrettyWood commented Jul 28, 2020

Hi @Gatux
Thanks a lot for your precious remarks! You're absolutely right!
i added format: regex and the doc.

Screen Shot 2020-07-29 at 1 26 58 AM

Do not hesitate to tell me if you see anything else wrong / to be modified

@PrettyWood PrettyWood force-pushed the fix/pattern-schema branch 2 times, most recently from 7ebb75c to 9148cc7 Compare July 29, 2020 07:29
@Gatux
Copy link

Gatux commented Jul 30, 2020

Hi @PrettyWood
Looks good to me ! Thanks !

@@ -643,7 +645,8 @@ def add_field_type_to_schema(field_type: Any, schema: Dict[str, Any]) -> None:
and then modifies the given `schema` with the information from that type.
"""
for type_, t_schema in field_class_to_schema:
if issubclass(field_type, type_):
# Fallback for `typing.Pattern` as it is not a valid class
if lenient_issubclass(field_type, type_) or field_type is type_ is Pattern:
Copy link
Member Author

Choose a reason for hiding this comment

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

We may have a better check later to support for example Pattern[str] but typing API changed so much that we'll probably first need some utils to retrieve the right informations depending on python version.
I feel like this is a good enough solution for the time being

Copy link
Contributor

Choose a reason for hiding this comment

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

#2399 also hits this
a python >= 3.9 solution is in #2392

@samuelcolvin samuelcolvin merged commit 55e78cc into pydantic:master Oct 8, 2020
@samuelcolvin
Copy link
Member

👍 thanks so much.

inabayuta added a commit to inabayuta/pydantic that referenced this pull request Oct 12, 2020
* commit '8ccc5708f120f65582cf9238592c6004efffc32d': (22 commits)
  remove data.json, fix pydantic#1992 (pydantic#1994)
  Include tests in source distributions (pydantic#1976)
  Fix const validator not running when class validators are present (pydantic#1957)
  Force fields.Undefined to be a singleton objectIn various places of the code, we compare directly to `fields.Undefined`since we assume it to be constant.When new models get created however, the object is deepcopied andis no longer identical with the original object.We therefore add `__copy__` and `__deepcopy__` methods to ensurethat the copied objects are actually the same original object. (pydantic#1981)
  test_config_file_settings_nornir: use less common env. var names (pydantic#1977)
  fix linting
  docs: fix typo (pydantic#1959)
  - fix typo in docs/index.md (pydantic#1921)
  Fix typo in docstring (pydantic#1866)
  add a __call__ stub to PyObject for mypy (pydantic#1849)
  remove strict_optional from sample mypy.ini in doc (pydantic#1830)
  docs: Fix incorrect description of copy method (pydantic#1821)
  feat(tools): add `parse_raw_as` util (pydantic#1813)
  Bump isort from 5.5.4 to 5.6.1 (pydantic#1980)
  feat(dotenv): support home directory relative paths (e.g. ~/.env) (pydantic#1804)
  combined uprev of dependencies (pydantic#1978)
  Bump pytest from 6.0.1 to 6.1.1 (pydantic#1965)
  clarify argument type to parse_file (pydantic#1795)
  Fix pydantic#1770 (pydantic#1771)
  fix(schema): add basic support of Pattern type in schema generation (pydantic#1768)
  ...

# Conflicts:
#	docs/index.md
#	docs/mypy_plugin.md
@PrettyWood PrettyWood deleted the fix/pattern-schema branch February 13, 2021 10:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
4 participants