Skip to content

Commit

Permalink
TypedDict
Browse files Browse the repository at this point in the history
  • Loading branch information
debonte committed Nov 5, 2024
1 parent b8b41c5 commit 9601065
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 21 deletions.
43 changes: 23 additions & 20 deletions packages/pyright-internal/src/analyzer/typedDicts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
ParamCategory,
ParseNodeType,
} from '../parser/parseNodes';
import { Tokenizer } from '../parser/tokenizer';
import { KeywordType } from '../parser/tokenizerTypes';
import * as AnalyzerNodeInfo from './analyzerNodeInfo';
import { ConstraintTracker } from './constraintTracker';
Expand Down Expand Up @@ -359,27 +360,29 @@ export function synthesizeTypedDictClassMethods(
}

entries.knownItems.forEach((entry, name) => {
FunctionType.addParam(
initOverride1,
FunctionParam.create(
ParamCategory.Simple,
entry.valueType,
FunctionParamFlags.TypeDeclared,
name,
entry.valueType
)
);
if (Tokenizer.isPythonIdentifier(name)) {
FunctionType.addParam(
initOverride1,
FunctionParam.create(
ParamCategory.Simple,
entry.valueType,
FunctionParamFlags.TypeDeclared,
name,
entry.valueType
)
);

FunctionType.addParam(
initOverride2,
FunctionParam.create(
ParamCategory.Simple,
entry.valueType,
FunctionParamFlags.TypeDeclared,
name,
entry.isRequired ? undefined : entry.valueType
)
);
FunctionType.addParam(
initOverride2,
FunctionParam.create(
ParamCategory.Simple,
entry.valueType,
FunctionParamFlags.TypeDeclared,
name,
entry.isRequired ? undefined : entry.valueType
)
);
}

if (!entry.isReadOnly) {
allEntriesAreReadOnly = false;
Expand Down
6 changes: 5 additions & 1 deletion packages/pyright-internal/src/tests/samples/typedDict6.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# This sample tests the type analyzer's handling of TypedDict
# "alternate syntax" defined in PEP 589.

from typing import NotRequired, Required, TypedDict
from typing import NotRequired, Required, TypedDict, reveal_type

Movie = TypedDict("Movie", {"name": str, "year": int})

Expand Down Expand Up @@ -81,3 +81,7 @@ def foo(unknown_str_value: str):
# This should generate an error because the name doesn't match.
# the arguments are missing.
Movie13 = TypedDict("NotMovie13", {"name": str, "year": int})


Movie14 = TypedDict("Movie14", {"@illegal identifier": int, "legal_identifier": int})
reveal_type(Movie14.__init__, expected_text="Overload[(self: Movie14, __map: Movie14, /, *, legal_identifier: int = ...) -> None, (self: Movie14, *, legal_identifier: int) -> None]")

0 comments on commit 9601065

Please sign in to comment.