-
-
Notifications
You must be signed in to change notification settings - Fork 674
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🐛 Fix equality check for custom classes (#979)
Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Sofie Van Landeghem <[email protected]> Co-authored-by: svlandeg <[email protected]> Co-authored-by: Sebastián Ramírez <[email protected]>
- Loading branch information
1 parent
bfb54c0
commit dc1bf3c
Showing
2 changed files
with
38 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters