Skip to content

Commit

Permalink
🐛 Fix equality check for custom classes (#979)
Browse files Browse the repository at this point in the history
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Co-authored-by: Sofie Van Landeghem <svlandeg@users.noreply.github.com>
Co-authored-by: svlandeg <svlandeg@github.com>
Co-authored-by: Sebastián Ramírez <tiangolo@gmail.com>
  • Loading branch information
5 people authored Nov 7, 2024
1 parent bfb54c0 commit dc1bf3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
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

0 comments on commit dc1bf3c

Please sign in to comment.