Skip to content

Commit

Permalink
More fun: object -> Any to allow more restrictive __diff__ implementa…
Browse files Browse the repository at this point in the history
…tions.
  • Loading branch information
Julian committed Dec 30, 2023
1 parent d7d0204 commit 41e4cda
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
6 changes: 3 additions & 3 deletions diff/_diff.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from difflib import ndiff
from typing import Protocol, TypeVar, overload, runtime_checkable
from typing import Any, Protocol, TypeVar, overload, runtime_checkable

from attrs import field, frozen

Expand All @@ -22,7 +22,7 @@ def explain(self) -> str:

@runtime_checkable
class Diffable(Protocol[D_co]):
def __diff__(self, other: object) -> D_co:
def __diff__(self, other: Any) -> D_co:
...


Expand All @@ -35,7 +35,7 @@ def explain(self) -> str:


@overload
def diff(one: Diffable[D_co], two: object) -> D_co | None:
def diff(one: Diffable[D_co], two: Any) -> D_co | None:
...


Expand Down
9 changes: 9 additions & 0 deletions diff/tests/typing/check_me_pyright.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,12 @@ def __diff__(self, other: object) -> ConcreteDifference:


difference: ConcreteDifference | None = diff(Something(), Something())


class PartiallyDiffable:
def __diff__(self, other: PartiallyDiffable) -> ConcreteDifference:
return ConcreteDifference()


partially = PartiallyDiffable()
difference: ConcreteDifference | None = diff(partially, partially)

0 comments on commit 41e4cda

Please sign in to comment.