Skip to content

Commit

Permalink
Added support for an unpacked tuple assignment within an instance met…
Browse files Browse the repository at this point in the history
…hod when the source of the assignment is the `self` parameter.
  • Loading branch information
msfterictraut committed Oct 13, 2021
1 parent 6c73337 commit 5422827
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2494,6 +2494,8 @@ export function createTypeEvaluator(importLookup: ImportLookup, evaluatorOptions
// entries at that location.
const unpackIndex = target.expressions.findIndex((expr) => expr.nodeType === ParseNodeType.Unpack);

type = makeTopLevelTypeVarsConcrete(type);

doForEachSubtype(type, (subtype) => {
// Is this subtype a tuple?
const tupleType = getSpecializedTupleType(subtype);
Expand Down
15 changes: 15 additions & 0 deletions packages/pyright-internal/src/tests/samples/namedTuples5.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# This sample tests the case where a NamedTuple object is referenced
# through a `self` parameter.

from typing import Literal, NamedTuple


class Fruit(NamedTuple):
name: str
cost: float

def new_cost(self, new_cost: float):
my_name, my_cost = self
t1: Literal["str"] = reveal_type(my_name)
t2: Literal["float"] = reveal_type(my_cost)
return Fruit(my_name, new_cost)
6 changes: 6 additions & 0 deletions packages/pyright-internal/src/tests/typeEvaluator1.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,12 @@ test('NamedTuples4', () => {
TestUtils.validateResults(analysisResults, 0);
});

test('NamedTuples5', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['namedTuples5.py']);

TestUtils.validateResults(analysisResults, 0);
});

test('Slots1', () => {
const analysisResults = TestUtils.typeAnalyzeSampleFiles(['slots1.py']);

Expand Down

0 comments on commit 5422827

Please sign in to comment.