Skip to content
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

unsafe variance: reword note and fix the notes code #452

Merged
merged 1 commit into from
May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## [Unreleased]
### Enhancements
- Removed `Any` from the typings for `re.Match` group functions. (#459)
### Fixes
- Fix unsafe variance note (#452)

## [1.8.0]
### Added
Expand Down
5 changes: 4 additions & 1 deletion mypy/message_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ def with_additional_msg(self, info: str) -> ErrorMessage:
"This usage of this covariant type variable is unsafe as an input parameter.",
codes.UNSAFE_VARIANCE,
)
UNSAFE_VARIANCE_NOTE = "If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'"
UNSAFE_VARIANCE_NOTE = ErrorMessage(
"If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored",
codes.UNSAFE_VARIANCE,
)
INCOMPATIBLE_IMPORT_OF: Final = ErrorMessage('Incompatible import of "{}"', code=codes.ASSIGNMENT)
FUNCTION_TYPE_EXPECTED: Final = ErrorMessage(
"Function is missing a type annotation", codes.NO_UNTYPED_DEF
Expand Down
11 changes: 9 additions & 2 deletions test-data/unit/check-based-unsafe-variance.test
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ from helper import T_out
from typing import Generic
class G(Generic[T_out]):
def f(self, t: T_out): ... # E: This usage of this covariant type variable is unsafe as an input parameter. [unsafe-variance] \
# N: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
# N: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored


[case testUnsafeVarianceContra]
from helper import T_in
from typing import Generic
class G(Generic[T_in]):
def f(self) -> T_in: ... # E: This usage of this contravariant type variable is unsafe as a return type. [unsafe-variance] \
# N: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
# N: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored


[case testIgnore]
from helper import T_in
from typing import Generic
class G(Generic[T_in]):
def f(self) -> T_in: ... # type: ignore[unsafe-variance]
8 changes: 4 additions & 4 deletions test-data/unit/check-functions.test
Original file line number Diff line number Diff line change
Expand Up @@ -2112,7 +2112,7 @@ class A(Generic[t]):
[builtins fixtures/bool.pyi]
[out]
main:5: error: This usage of this covariant type variable is unsafe as an input parameter.
main:5: note: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
main:5: note: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored

[case testRejectCovariantArgumentSplitLine]
from typing import TypeVar, Generic
Expand All @@ -2125,7 +2125,7 @@ class A(Generic[t]):
[builtins fixtures/bool.pyi]
[out]
main:6: error: This usage of this covariant type variable is unsafe as an input parameter.
main:6: note: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
main:6: note: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored

[case testRejectCovariantArgumentInLambda]
from typing import TypeVar, Generic, Callable
Expand All @@ -2139,7 +2139,7 @@ class Thing(Generic[t]):
[builtins fixtures/bool.pyi]
[out]
main:8: error: This usage of this covariant type variable is unsafe as an input parameter.
main:8: note: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
main:8: note: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored

[case testRejectCovariantArgumentInLambdaSplitLine]
from typing import TypeVar, Generic, Callable
Expand All @@ -2154,7 +2154,7 @@ class A(Generic[t]):
[builtins fixtures/bool.pyi]
[out]
main:5: error: This usage of this contravariant type variable is unsafe as a return type.
main:5: note: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
main:5: note: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored

[case testAcceptCovariantReturnType]
from typing import TypeVar, Generic
Expand Down
4 changes: 2 additions & 2 deletions test-data/unit/check-protocols.test
Original file line number Diff line number Diff line change
Expand Up @@ -844,10 +844,10 @@ T_contra = TypeVar('T_contra', contravariant=True)

class Proto(Protocol[T_co, T_contra]): # type: ignore
def one(self, x: T_co) -> None: # E: This usage of this covariant type variable is unsafe as an input parameter. \
# N: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
# N: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored
pass
def other(self) -> T_contra: # E: This usage of this contravariant type variable is unsafe as a return type. \
# N: If this is intentional and you know what you are doing, you can ignore this line with 'unsafe-variance'
# N: If you are using the value in a 'variance safe' way (not storing or retrieving values), this error could be ignored
pass

# Check that we respect user overrides of variance after the errors are reported
Expand Down