variance for parameter specification and type variable tuples#1771
variance for parameter specification and type variable tuples#1771KotlinIsland wants to merge 1 commit into
Conversation
a63766e to
05505d8
Compare
05505d8 to
a75256a
Compare
|
Diff from mypy_primer, showing the effect of this PR on open source code: core (https://github.com/home-assistant/core)
- Type parameter "_P@HassJob" is invariant, but "(_: datetime | None = None)" is not the same as "(datetime)"
+ .../projects/core/homeassistant/components/google_assistant/report_state.py:196:37 - warning: Argument type is partially unknown
+ Argument corresponds to parameter "action" in function "async_call_later"
+ Argument type is "HassJob[(_now: Unknown), Coroutine[Any, Any, None] | None]" (reportUnknownArgumentType)
- Type parameter "_P@HassJob" is invariant, but "(_: datetime | None = None)" is not the same as "(datetime)"
- Type parameter "_P@HassJob" is invariant, but "(_now: datetime | None = None)" is not the same as "(datetime)"
- Type parameter "_P@HassJob" is invariant, but "(_: Exception | None = None)" is not the same as "(datetime)"
+ Type parameter "_P@HassJob" is covariant, but "(_: Exception | None = None)" is not a subtype of "(datetime)"
+ Type "(_: Exception | None = None)" is not assignable to type "(datetime)"
+ Parameter 1: type "datetime" is incompatible with type "Exception | None"
- .../projects/core/homeassistant/components/wemo/__init__.py:289:17 - error: Argument of type "HassJob[(datetime), None]" cannot be assigned to parameter "action" of type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))" in function "async_call_later"
+ .../projects/core/homeassistant/components/wemo/__init__.py:289:17 - error: Argument of type "HassJob[(event_time: datetime), None] | HassJob[(datetime), None]" cannot be assigned to parameter "action" of type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))" in function "async_call_later"
+ Type "HassJob[(event_time: datetime), None] | HassJob[(datetime), None]" is not assignable to type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))"
- Type "HassJob[(datetime), None]" is not assignable to type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))"
+ Type "HassJob[(datetime), None]" is not assignable to type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))"
- "HassJob[(datetime), None]" is not assignable to "HassJob[(datetime), Coroutine[Any, Any, None] | None]"
+ "HassJob[(datetime), None]" is not assignable to "HassJob[(datetime), Coroutine[Any, Any, None] | None]"
- Type parameter "_R_co@HassJob" is invariant, but "None" is not the same as "Coroutine[Any, Any, None] | None"
+ Type parameter "_R_co@HassJob" is invariant, but "None" is not the same as "Coroutine[Any, Any, None] | None"
- Type "HassJob[(datetime), None]" is not assignable to type "(datetime) -> (Coroutine[Any, Any, None] | None)" (reportArgumentType)
+ Type "HassJob[(datetime), None]" is not assignable to type "(datetime) -> (Coroutine[Any, Any, None] | None)" (reportArgumentType)
+ .../projects/core/homeassistant/components/yeelight/scanner.py:186:28 - warning: Argument type is partially unknown
+ Argument corresponds to parameter "action" in function "async_call_later"
+ Argument type is "HassJob[(*_: Unknown), Coroutine[Any, Any, None] | None]" (reportUnknownArgumentType)
- .../projects/core/homeassistant/helpers/event.py:1819:13 - error: Argument of type "HassJob[(datetime), None]" cannot be assigned to parameter "action" of type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))" in function "async_track_point_in_utc_time"
+ .../projects/core/homeassistant/helpers/event.py:1819:13 - error: Argument of type "HassJob[(_: datetime), None]" cannot be assigned to parameter "action" of type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))" in function "async_track_point_in_utc_time"
- Type "HassJob[(datetime), None]" is not assignable to type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))"
+ Type "HassJob[(_: datetime), None]" is not assignable to type "HassJob[(datetime), Coroutine[Any, Any, None] | None] | ((datetime) -> (Coroutine[Any, Any, None] | None))"
- "HassJob[(datetime), None]" is not assignable to "HassJob[(datetime), Coroutine[Any, Any, None] | None]"
+ "HassJob[(_: datetime), None]" is not assignable to "HassJob[(datetime), Coroutine[Any, Any, None] | None]"
- Type "HassJob[(datetime), None]" is not assignable to type "(datetime) -> (Coroutine[Any, Any, None] | None)" (reportArgumentType)
+ Type "HassJob[(_: datetime), None]" is not assignable to type "(datetime) -> (Coroutine[Any, Any, None] | None)" (reportArgumentType)
- 33917 errors, 397968 warnings, 0 notes
+ 33917 errors, 397970 warnings, 0 notes
|
|
what issue is this fixing? |
the issue that tvt/param spec variance isn't supported |
| if (paramName === 'covariant') { | ||
| if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression!)) { | ||
| if ( | ||
| paramSpec.shared.declaredVariance === Variance.Contravariant || | ||
| paramSpec.shared.declaredVariance === Variance.Auto | ||
| ) { | ||
| addDiagnostic( | ||
| DiagnosticRule.reportGeneralTypeIssues, | ||
| LocMessage.typeVarVariance(), | ||
| argList[i].valueExpression! | ||
| ); | ||
| } else { | ||
| paramSpec.shared.declaredVariance = Variance.Covariant; | ||
| } | ||
| } | ||
| } else if (paramName === 'contravariant') { | ||
| if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression!)) { | ||
| if ( | ||
| paramSpec.shared.declaredVariance === Variance.Covariant || | ||
| paramSpec.shared.declaredVariance === Variance.Auto | ||
| ) { | ||
| addDiagnostic( | ||
| DiagnosticRule.reportGeneralTypeIssues, | ||
| LocMessage.typeVarVariance(), | ||
| argList[i].valueExpression! | ||
| ); | ||
| } else { | ||
| paramSpec.shared.declaredVariance = Variance.Contravariant; | ||
| } | ||
| } | ||
| } else if (paramName === 'infer_variance') { | ||
| if (argList[i].valueExpression && getBooleanValue(argList[i].valueExpression!)) { | ||
| if ( | ||
| paramSpec.shared.declaredVariance === Variance.Covariant || | ||
| paramSpec.shared.declaredVariance === Variance.Contravariant | ||
| ) { | ||
| addDiagnostic( | ||
| DiagnosticRule.reportGeneralTypeIssues, | ||
| LocMessage.typeVarVariance(), | ||
| argList[i].valueExpression! | ||
| ); | ||
| } else { | ||
| paramSpec.shared.declaredVariance = Variance.Auto; | ||
| } | ||
| } | ||
| } else if (paramName === 'default') { |
| class ShouldBeContravariant1[**OutP]: | ||
| def f(self) -> Callable[OutP, None]: ... |
There was a problem hiding this comment.
should also have a test for the most common usage:
class ShouldBeContravariant2[**OutP]:
def f(self, *args: P.args, **kwargs: P.kwargs): ...| P_co = ParamSpec("P_co", covariant=True) | ||
| P_contra = ParamSpec("P_contra", contravariant=True) | ||
| P_infer = ParamSpec("P_infer", infer_variance=True) |
There was a problem hiding this comment.
is it just to make sure theres no error on the arguments? if so should add a comment
| Ts_co = TypeVarTuple("Ts_co", covariant=True) | ||
| Ts_contra = TypeVarTuple("Ts_contra", contravariant=True) | ||
| Ts_infer = TypeVarTuple("Ts_infer", infer_variance=True) |
There was a problem hiding this comment.
comment saying why these unused typevar variables are here
There was a problem hiding this comment.
honestly i have never encountered a single situation where a typevartuple has been useful. could you please provide a realistic example of when anyone would ever use one, and how it could benefit from supporting variance? otherwise i dont see why we should even bother to add variance support to them or touch them in any way.
it sounds like it was just scope creeped in your discourse thread about ParamSpecs for no reason
poc for python/typing#2215