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

Implement deprecated field in json schema #9298

Merged
merged 7 commits into from May 22, 2024

Conversation

NeevCohen
Copy link
Contributor

@NeevCohen NeevCohen commented Apr 21, 2024

Change Summary

Reflect model depreciation in json schema

Related issue number

Related to #8922

Checklist

  • The pull request title is a good summary of the changes - it will be used in the changelog
  • Unit tests for the changes exist
  • Tests pass on CI
  • Documentation reflects the changes where applicable
  • My PR is ready to review, please add a comment including the phrase "please review" to assign reviewers

Selected Reviewer: @Kludex

Copy link

codspeed-hq bot commented Apr 21, 2024

CodSpeed Performance Report

Merging #9298 will not alter performance

Comparing NeevCohen:feature/json-schema-deprecated (1a3b357) with main (c488508)

Summary

✅ 13 untouched benchmarks

@NeevCohen
Copy link
Contributor Author

Please review

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

@NeevCohen,

Nice work, as always! Just one minor change request, but looks great otherwise!

pydantic/json_schema.py Show resolved Hide resolved
Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Oops, meant to 'request changes'

@NeevCohen
Copy link
Contributor Author

@sydney-runkle I added a check for a truthy value in the __deprecated__ attribute

return json_schema

@staticmethod
def _should_mark_class_as_deprecated(cls) -> bool:
return hasattr(cls, '__deprecated__') and getattr(cls, '__deprecated__')
Copy link
Member

Choose a reason for hiding this comment

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

getattr is enough but you can make it more explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@PrettyWood Sure. Do you think bool(getattr(cls, '__deprecated__')) is fine?

Copy link
Member

Choose a reason for hiding this comment

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

LGTM once we decide on the syntax for this line!

Copy link
Member

Choose a reason for hiding this comment

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

I just made a remark regarding the code with hasattr + getattr.
I now took a bit of time to understand the context and reading https://peps.python.org/pep-0702/ it seems you only care about the presence of the field
I can decide to write

@deprecated("")
class A: ...

and I'll have A.__deprecated__ = "", which will be falsy and still valid.

So in the end I guess the initial approach with just return hasattr(cls, '__deprecated__') is the best

Copy link
Member

Choose a reason for hiding this comment

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

Screenshot 2024-05-15 at 15 58 46

Copy link
Member

Choose a reason for hiding this comment

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

What about a hasattr and then a false check? @NeevCohen @PrettyWood

Copy link
Contributor Author

@NeevCohen NeevCohen May 21, 2024

Choose a reason for hiding this comment

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

I guess that's reasonable, though @deprecated takes a str as a parameter, if anything else is passed in an exception is raised. The following doesn't work

from typing import deprecated
from pydantic import BaseModel

@deprecated(False)
class Model(BaseModel):
    pass

Raises the following exception

TypeError: Expected an object of type str for 'message', not 'bool'

I guess someone could manually set a __deprecated__ attribute on the class to False, though I'm not sure that's a use case as far as we're concerned.

I guess just the simple hasattr check is the best choice here.

@sydney-runkle @PrettyWood thoughts?

Copy link
Member

Choose a reason for hiding this comment

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

+1. As I said earlier "So in the end I guess the initial approach with just return hasattr(cls, 'deprecated') is the best"

Copy link
Member

@sydney-runkle sydney-runkle left a comment

Choose a reason for hiding this comment

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

Looks great, thanks @NeevCohen!

@sydney-runkle sydney-runkle merged commit 5259945 into pydantic:main May 22, 2024
52 checks passed
@NeevCohen NeevCohen deleted the feature/json-schema-deprecated branch May 22, 2024 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants