Skip to content
Myr

🐛 Fix equality check for custom classes (#979)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Aryaz Eghbali · committed by GitHub · parent bfb54c0 · Browse files · 2 files+38 added−2 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Trailers

  • 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>

Diff stats

2 files+38 added−2 removed

  • tests:+36 added−0 removed
  • other:+2 added−2 removed

2 files+38 added−2 removed

added:tests/test_param_meta_empty.py+36 added
1import typer2from typer.testing import CliRunner34runner = CliRunner()567def test_default_with_class_with_custom_eq():8 app = typer.Typer()910 from typer.models import ParamMeta1112 class StupidClass:13 def __init__(self, a):14 self.a = a1516 def __eq__(self, other) -> bool:17 if other is ParamMeta.empty:18 return True19 try:20 return self.a == other.a21 except Exception:22 return False2324 def __ne__(self, other: object) -> bool:25 return not self.__eq__(other)2627 @app.command()28 def cmd(val=StupidClass(42)):29 print(val)3031 assert StupidClass(666) == ParamMeta.empty32 assert StupidClass(666) != StupidClass(1)3334 result = runner.invoke(app)35 assert result.exit_code == 0, result.output36 assert "StupidClass" in result.output
modified:typer/main.py+2 added-2 removed
819 unmodified lines
820 if parameter_info.default == Required:821 required = True822 else:823 default_value = parameter_info.default824 elif param.default == Required or param.default == param.empty:824 elif param.default == Required or param.default is param.empty:825 required = True826 parameter_info = ArgumentInfo()827 else:828 default_value = param.default829 parameter_info = OptionInfo()830 annotation: Any831 if not param.annotation == param.empty:831 if param.annotation is not param.empty:832 annotation = param.annotation833 else:834 annotation = str835 main_type = annotation
245 unmodified lines