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 equality check for custom classes #979

Merged
merged 5 commits into from
Nov 7, 2024
Merged

Conversation

AryazE
Copy link
Contributor

@AryazE AryazE commented Sep 8, 2024

Similar to This issue in rich, comparing types with == could be problematic. The canonical is would fix this problem.

@svlandeg svlandeg added the bug Something isn't working label Sep 9, 2024
@svlandeg svlandeg self-assigned this Sep 9, 2024
@svlandeg svlandeg changed the title Fixed issue with custom classes 🐛 Fix equality check for custom classes Sep 9, 2024
Copy link
Member

@svlandeg svlandeg left a comment

Choose a reason for hiding this comment

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

Thanks for the PR!

Changing the check from equality to identity check seems correct to me, also considering that the official Python documentation on inspect.Parameter also includes this snippet:

if param.default is param.empty

The test you included fails on master and succeeds on this PR. I only added 2 more lines to ensure 100% code coverage.

@svlandeg svlandeg removed their assignment Sep 9, 2024
@@ -76,3 +76,35 @@ def cmd(force: Annotated[bool, typer.Option("--force")] = False):
result = runner.invoke(app, ["--force"])
assert result.exit_code == 0, result.output
assert "Forcing operation" in result.output


def test_default_with_class_with_custom_eq():
Copy link
Member

Choose a reason for hiding this comment

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

Tiangolo: we might want this test in a different location in the test suite?

Copy link
Member

Choose a reason for hiding this comment

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

Good point! I just moved it to another file. Not sure about the name, but it will work for now. 🤷

Copy link
Member

@tiangolo tiangolo left a comment

Choose a reason for hiding this comment

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

Cool, thanks! 🚀

I don't see an obvious use case where this would be a problem as types in parameters for Typer are from the default Python types, but anyway, it doesn't hurt having it. 🤓

Thanks @AryazE!

Thanks @svlandeg for the help! 🙇

@tiangolo tiangolo enabled auto-merge (squash) November 7, 2024 17:22
@tiangolo tiangolo merged commit dc1bf3c into fastapi:master Nov 7, 2024
23 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants