You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I created two NewTypes from a third party class which didn't have stubs. When using these NewTypes with a generic function, mypy infers the wrong result type, mixing up the two NewTypes.
To Reproduce
fromsome_pkgimportSomeClsRight=NewType("Right", SomeCls)
Wrong=NewType("Wrong", SomeCls)
T=TypeVar("T", Right, Wrong)
defgeneric_fn(data_type: type[T]) ->T: ...
assert_type(generic_fn(Right), Right) # error: Expression is of type "Wrong", not "Right"
Mypy should allow for NewTypes made from Any and treat them as separate types, like other NewTypes are. (pyright handles this correctly)
Since 3.11, mypy should treat Any as subclassable, and therefore usable with NewType. (Some past discussion here: #12840 (comment))
Actual Behavior
Mypy mixes up the NewTypes made from Any.
Your Environment
Mypy version used: 1.2, 1.5.1, master
Mypy command-line flags: N/A
Mypy configuration options from mypy.ini (and other config files): N/A
Python version used: 3.10, 3.11
Non-solutions
In #12757 (comment) it was suggested to use NewType with object, but
Right = NewType("Right", cast(object, SomeCls)) gives a misc mypy error, and also even if you do Right = NewType("Right", object) then although the generic typechecking will work, subsequent usages of the NewType instances lose all their attributes, when they should be usable like Any.
The text was updated successfully, but these errors were encountered:
@Hnasar, contrary to what you say above, pyright does not handle this case as you are hoping. Like mypy, pyright does not treat the types as distinct. When you derive a class from Any, many assumptions within a type checker no longer hold true.
If you want Right and Wrong to be treated as Any, you should avoid using the NewType. If you want them to be treated like distinct types (but not like Any), then you can use a pattern like this:
Bug Report
I created two NewTypes from a third party class which didn't have stubs. When using these NewTypes with a generic function, mypy infers the wrong result type, mixing up the two NewTypes.
To Reproduce
https://mypy-play.net/?mypy=master&python=3.11&gist=adc223e9872d9a7cae12ada10634c014
Expected Behavior
Mypy should allow for NewTypes made from
Any
and treat them as separate types, like other NewTypes are. (pyright handles this correctly)Since 3.11, mypy should treat Any as subclassable, and therefore usable with NewType. (Some past discussion here: #12840 (comment))
Actual Behavior
Mypy mixes up the NewTypes made from
Any
.Your Environment
mypy.ini
(and other config files): N/ANon-solutions
In #12757 (comment) it was suggested to use NewType with object, but
Right = NewType("Right", cast(object, SomeCls))
gives amisc
mypy error, and also even if you doRight = NewType("Right", object)
then although the generic typechecking will work, subsequent usages of the NewType instances lose all their attributes, when they should be usable likeAny
.The text was updated successfully, but these errors were encountered: