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
TypeVar(…, bound=RealLike)
creates problems
#10
Comments
TypeVar(…, bound=RealLike)
creates problems
@antonagestam, I still have no idea know why I tried the following with some success, although I'm not sure if this does what you want: diff --git a/src/phantom/interval.py b/src/phantom/interval.py
index e795106..55db040 100644
--- a/src/phantom/interval.py
+++ b/src/phantom/interval.py
@@ -26,7 +26,6 @@ from __future__ import annotations
from typing import Any
from typing import TypeVar
-from numerary.types import IntegralLike
from numerary.types import RealLike
from typing_extensions import Final
from typing_extensions import Protocol
@@ -37,12 +36,12 @@ from .predicates import interval
from .schema import Schema
from .utils import resolve_class_attr
-N = TypeVar("N", bound=RealLike[IntegralLike[int]])
+N = TypeVar("N", covariant=True)
Derived = TypeVar("Derived", bound="Interval")
class IntervalCheck(Protocol):
- def __call__(self, a: N, b: N) -> Predicate[N]:
+ def __call__(self, a: RealLike[N], b: RealLike[N]) -> Predicate[RealLike[N]]:
...
diff --git a/src/phantom/predicates/base.py b/src/phantom/predicates/base.py
index d13a76e..10c01b7 100644
--- a/src/phantom/predicates/base.py
+++ b/src/phantom/predicates/base.py
@@ -1,6 +1,10 @@
-from typing import Callable
from typing import TypeVar
+from typing_extensions import Protocol
+
T = TypeVar("T", bound=object, contravariant=True)
-Predicate = Callable[[T], bool]
+
+class Predicate(Protocol[T]):
+ def __call__(self, __: T, /) -> bool:
+ ... I also tried this: diff --git a/src/phantom/interval.py b/src/phantom/interval.py
index e795106..a62fd3d 100644
--- a/src/phantom/interval.py
+++ b/src/phantom/interval.py
@@ -26,7 +26,6 @@ from __future__ import annotations
from typing import Any
from typing import TypeVar
-from numerary.types import IntegralLike
from numerary.types import RealLike
from typing_extensions import Final
from typing_extensions import Protocol
@@ -37,11 +36,11 @@ from .predicates import interval
from .schema import Schema
from .utils import resolve_class_attr
-N = TypeVar("N", bound=RealLike[IntegralLike[int]])
+N = TypeVar("N", bound=RealLike, contravariant=True)
Derived = TypeVar("Derived", bound="Interval")
-class IntervalCheck(Protocol):
+class IntervalCheck(Protocol[N]):
def __call__(self, a: N, b: N) -> Predicate[N]:
...
In both cases, I can eliminate the error you got, but I get a new one:
Again, I'm not sure I follow everything that code does, nor am I sure if any of the above is helpful. I'll continue to look into this, but if there's a way to reduce it, that might help? |
Hmm, that change doesn't really make sense to me. The original signature But the new signature So I don't think I want to apply that change to the type var usage. Is changing
That's a known mypy bug, I think it has been fixed recently: python/mypy#4660 Which mypy version are you running with? |
I finally have an update! I've since made some changes to The bad news: Other news: The good news: It appears this is no longer an issue, at least not with Mypy 0.960 (and possibly other recent versions). Here's my diff to antonagestam/phantom-types@0461b69 : diff --git a/src/phantom/interval.py b/src/phantom/interval.py
index 0abe9ba..31dbe6e 100644
--- a/src/phantom/interval.py
+++ b/src/phantom/interval.py
@@ -26,7 +26,6 @@ from __future__ import annotations
from typing import Any
from typing import TypeVar
-from numerary.types import IntegralLike
from numerary.types import RealLike
from typing_extensions import Final
from typing_extensions import Protocol
@@ -37,7 +36,7 @@ from .predicates import interval
from .schema import Schema
from .utils import resolve_class_attr
-N = TypeVar("N", bound=RealLike[IntegralLike[int]])
+N = TypeVar("N", bound=RealLike)
Derived = TypeVar("Derived", bound="Interval")
Here's how it plays (from the $ pip list | grep numerary
numerary 0.4.0
$ mypy --version
mypy 0.960 (compiled: yes)
$ mypy src/phantom/interval.py
Success: no issues found in 1 source file
$ pre-commit run mypy --all-files
mypy.....................................................................Passed I'm pretty sure this was the result of changes to Mypy rather than changes to |
First hinted at by @antonagestam in antonagestam/phantom-types#179 (comment), which led to an interesting work-around that
numerary
probably shouldn't impose, if it can avoid it.The text was updated successfully, but these errors were encountered: