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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 36 additions & 0 deletions tests/test_param_meta_empty.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import typer
from typer.testing import CliRunner

runner = CliRunner()


def test_default_with_class_with_custom_eq():
app = typer.Typer()

from typer.models import ParamMeta

class StupidClass:
def __init__(self, a):
self.a = a

def __eq__(self, other) -> bool:
if other is ParamMeta.empty:
return True
try:
return self.a == other.a
except Exception:
return False

def __ne__(self, other: object) -> bool:
return not self.__eq__(other)

@app.command()
def cmd(val=StupidClass(42)):
print(val)

assert StupidClass(666) == ParamMeta.empty
assert StupidClass(666) != StupidClass(1)

result = runner.invoke(app)
assert result.exit_code == 0, result.output
assert "StupidClass" in result.output
4 changes: 2 additions & 2 deletions typer/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -821,14 +821,14 @@ def get_click_param(
required = True
else:
default_value = parameter_info.default
elif param.default == Required or param.default == param.empty:
elif param.default == Required or param.default is param.empty:
required = True
parameter_info = ArgumentInfo()
else:
default_value = param.default
parameter_info = OptionInfo()
annotation: Any
if not param.annotation == param.empty:
if param.annotation is not param.empty:
annotation = param.annotation
else:
annotation = str
Expand Down
Loading