Explain that the type is invariant and give notes for how to solve the issue.
(
notes: list[str], arg_type: Instance, expected_type: Instance
)
| 3397 | |
| 3398 | |
| 3399 | def append_invariance_notes( |
| 3400 | notes: list[str], arg_type: Instance, expected_type: Instance |
| 3401 | ) -> list[str]: |
| 3402 | """Explain that the type is invariant and give notes for how to solve the issue.""" |
| 3403 | invariant_type = "" |
| 3404 | covariant_suggestion = "" |
| 3405 | if ( |
| 3406 | arg_type.type.fullname == "builtins.list" |
| 3407 | and expected_type.type.fullname == "builtins.list" |
| 3408 | and is_subtype(arg_type.args[0], expected_type.args[0]) |
| 3409 | ): |
| 3410 | invariant_type = "list" |
| 3411 | covariant_suggestion = 'Consider using "Sequence" instead, which is covariant' |
| 3412 | elif ( |
| 3413 | arg_type.type.fullname == "builtins.dict" |
| 3414 | and expected_type.type.fullname == "builtins.dict" |
| 3415 | and is_same_type(arg_type.args[0], expected_type.args[0]) |
| 3416 | and is_subtype(arg_type.args[1], expected_type.args[1]) |
| 3417 | ): |
| 3418 | invariant_type = "dict" |
| 3419 | covariant_suggestion = ( |
| 3420 | 'Consider using "Mapping" instead, which is covariant in the value type' |
| 3421 | ) |
| 3422 | if invariant_type and covariant_suggestion: |
| 3423 | notes.append( |
| 3424 | f'"{invariant_type}" is invariant -- see ' |
| 3425 | + "https://mypy.readthedocs.io/en/stable/common_issues.html#variance" |
| 3426 | ) |
| 3427 | notes.append(covariant_suggestion) |
| 3428 | return notes |
| 3429 | |
| 3430 | |
| 3431 | def append_union_note( |
no test coverage detected
searching dependent graphs…