Skip to content

Commit

Permalink
Changed the type of __path__ from Iterable[str] to `MutableSequen…
Browse files Browse the repository at this point in the history
…ce[str]`. Both are correct, but the latter is more precise. This addresses #10058. (#10062)
  • Loading branch information
erictraut authored Mar 11, 2025
1 parent bad0c19 commit ae5479d
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 8 deletions.
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/analyzer/binder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ export class Binder extends ParseTreeWalker {
this._addImplicitSymbolToCurrentScope('__loader__', node, 'Any');
this._addImplicitSymbolToCurrentScope('__package__', node, 'str | None');
this._addImplicitSymbolToCurrentScope('__spec__', node, 'Any');
this._addImplicitSymbolToCurrentScope('__path__', node, 'Iterable[str]');
this._addImplicitSymbolToCurrentScope('__path__', node, 'MutableSequence[str]');
this._addImplicitSymbolToCurrentScope('__file__', node, 'str');
this._addImplicitSymbolToCurrentScope('__cached__', node, 'str');
this._addImplicitSymbolToCurrentScope('__dict__', node, 'Dict[str, Any]');
Expand Down
9 changes: 8 additions & 1 deletion packages/pyright-internal/src/analyzer/declaration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,14 @@ export const enum DeclarationType {
Alias,
}

export type IntrinsicType = 'Any' | 'str' | 'str | None' | 'int' | 'Iterable[str]' | 'type[self]' | 'Dict[str, Any]';
export type IntrinsicType =
| 'Any'
| 'str'
| 'str | None'
| 'int'
| 'MutableSequence[str]'
| 'type[self]'
| 'Dict[str, Any]';

export interface DeclarationBase {
// Category of this symbol (function, variable, etc.).
Expand Down
8 changes: 4 additions & 4 deletions packages/pyright-internal/src/analyzer/typeEvaluator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22164,11 +22164,11 @@ export function createTypeEvaluator(
return { type: intType };
}

if (declaration.intrinsicType === 'Iterable[str]') {
const iterableType = getBuiltInType(declaration.node, 'Iterable');
if (isInstantiableClass(iterableType)) {
if (declaration.intrinsicType === 'MutableSequence[str]') {
const sequenceType = getBuiltInType(declaration.node, 'MutableSequence');
if (isInstantiableClass(sequenceType)) {
return {
type: ClassType.cloneAsInstance(ClassType.specialize(iterableType, [strType])),
type: ClassType.cloneAsInstance(ClassType.specialize(sequenceType, [strType])),
};
}
}
Expand Down
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/import1.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the type analyzer's handling of the built-in
# __import__ function.

reveal_type(__path__, expected_text="Iterable[str]")
reveal_type(__path__, expected_text="MutableSequence[str]")

# This should not generate a type error.
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
2 changes: 1 addition & 1 deletion packages/pyright-internal/src/tests/samples/module3.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
reveal_type(datetime.__loader__, expected_text="Any")
reveal_type(datetime.__package__, expected_text="str | None")
reveal_type(datetime.__spec__, expected_text="Any")
reveal_type(datetime.__path__, expected_text="Iterable[str]")
reveal_type(datetime.__path__, expected_text="MutableSequence[str]")
reveal_type(datetime.__file__, expected_text="str")
reveal_type(datetime.__cached__, expected_text="str")
reveal_type(datetime.__dict__, expected_text="dict[str, Any]")
Expand Down

0 comments on commit ae5479d

Please sign in to comment.