Added keyword-only check to dataclasess __match_args__ (#2615)

* Add keyword-only check to dataclasess `__match_args__`

* Fixed tests added in add keyword-only check to dataclasess `__match_args__`
This commit is contained in:
Hubert 2021-11-25 20:13:26 +01:00 committed by GitHub
parent b269ee9a85
commit 736e41c63c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 1 deletions

View File

@ -406,7 +406,7 @@ export function synthesizeDataClassMethods(
) {
const matchArgsNames: string[] = [];
fullDataClassEntries.forEach((entry) => {
if (entry.includeInInit) {
if (entry.includeInInit && !entry.isKeywordOnly) {
// Use the field name, not its alias (if it has one).
matchArgsNames.push(entry.name);
}

View File

@ -0,0 +1,17 @@
from dataclasses import dataclass, field
from typing import Literal
@dataclass
class Point:
optional: int | None = field(default=None, kw_only=True)
x: int
y: int
obj = Point(1, 2)
match obj:
case Point(x, y, optional=opt):
t_v1: Literal["int"] = reveal_type(x)
t_v2: Literal["int"] = reveal_type(y)
t_v3: Literal["int | None"] = reveal_type(opt)
distance = (x ** 2 + y ** 2) ** 0.5

View File

@ -659,6 +659,14 @@ test('Match7', () => {
TestUtils.validateResults(analysisResults, 2);
});
test('Match8', () => {
const configOptions = new ConfigOptions('.');
configOptions.defaultPythonVersion = PythonVersion.V3_10;
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['match8.py'], configOptions);
TestUtils.validateResults(analysisResults, 0);
});
test('List1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['list1.py']);
TestUtils.validateResults(analysisResults, 0);