PY-76892 Conformance test failure: typeddicts_class_syntax.py

Check for unexpected __init_subclass__ parameter

GitOrigin-RevId: bb6dd8975c14bf6758f6deaf73b6aead02003233
This commit is contained in:
Petr
2024-11-29 20:35:12 +00:00
committed by intellij-monorepo-bot
parent 360699a959
commit 2273799954
3 changed files with 21 additions and 4 deletions
@@ -1095,6 +1095,7 @@ INSP.typeddict.qualifiers.cannot.be.used.outside.typeddict.definition=''{0}'' ca
INSP.typeddict.required.and.not.required.cannot.be.nested=Required[] and NotRequired[] cannot be nested
INSP.typeddict.read.only.cannot.be.nested=ReadOnly[] cannot be nested
INSP.typeddict.required.notrequired.must.have.exactly.one.type.argument=''{0}'' must have exactly one type argument
INSP.typeddict.unexpected.argument.for.__init_subclass__.of.TypedDict=Unexpected argument ''{0}'' for __init_subclass__ of TypedDict
# PyTypeHintsInspection
INSP.NAME.type.hints=Invalid type hints definitions and usages
@@ -69,14 +69,23 @@ class PyTypedDictInspection : PyInspection() {
override fun visitPyArgumentList(node: PyArgumentList) {
if (node.parent is PyClass && PyTypedDictTypeProvider.isTypingTypedDictInheritor(node.parent as PyClass, myTypeEvalContext)) {
val arguments = node.arguments
for (argument in arguments) {
for (argument in node.arguments) {
val type = myTypeEvalContext.getType(argument)
if (!isValidSuperclass(argument, type)) {
registerProblem(argument, PyPsiBundle.message("INSP.typeddict.typeddict.cannot.inherit.from.non.typeddict.base.class"))
}
if (argument is PyKeywordArgument && argument.keyword == TYPED_DICT_TOTAL_PARAMETER && argument.valueExpression != null) {
checkValidTotality(argument.valueExpression!!)
if (argument is PyKeywordArgument) {
val keyword = argument.keyword
if (keyword == TYPED_DICT_TOTAL_PARAMETER) {
val valueExpression = argument.valueExpression
if (valueExpression != null) {
checkValidTotality(valueExpression)
}
}
else if (keyword != PyNames.METACLASS) {
registerProblem(argument,
PyPsiBundle.message("INSP.typeddict.unexpected.argument.for.__init_subclass__.of.TypedDict", keyword))
}
}
}
}
@@ -242,6 +242,13 @@ public class PyTypedDictInspectionTest extends PyInspectionTestCase {
"X = TypedDict('X', {'x': int}, total=<warning descr=\"Value of 'total' must be True or False\">1</warning>)");
}
public void testUnexpectedInitClassArgument() {
doTestByText("""
from typing import TypedDict
class A(TypedDict, <warning descr="Unexpected argument 'ab' for __init_subclass__ of TypedDict">ab=False</warning>):
i: int""");
}
public void testGetWithIncorrectKeyType() {
doTestByText("""
from typing import TypedDict