Skip to content

Commit

Permalink
Disable TypedDict for mypy
Browse files Browse the repository at this point in the history
  • Loading branch information
julienp committed May 15, 2024
1 parent 7138664 commit 18d2079
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 21 deletions.
24 changes: 21 additions & 3 deletions pkg/codegen/python/gen.go
Original file line number Diff line number Diff line change
Expand Up @@ -421,9 +421,9 @@ func (mod *modContext) generateCommonImports(w io.Writer, imports imports, typin
fmt.Fprintf(w, "from typing import %s\n", strings.Join(typingImports, ", "))
if mod.typedDictArgs {
fmt.Fprintf(w, "if sys.version_info >= (3, 11):\n")
fmt.Fprintf(w, " from typing import NotRequired, TypedDict\n")
fmt.Fprintf(w, " from typing import NotRequired, TypedDict, TypeAlias\n")
fmt.Fprintf(w, "else:\n")
fmt.Fprintf(w, " from typing_extensions import NotRequired, TypedDict\n")
fmt.Fprintf(w, " from typing_extensions import NotRequired, TypedDict, TypeAlias\n")
}
fmt.Fprintf(w, "from %s import _utilities\n", relImport)
for _, imp := range imports.strings() {
Expand Down Expand Up @@ -1074,6 +1074,10 @@ func (mod *modContext) genTypes(dir string, fs codegen.Fs) error {
}
fmt.Fprintf(w, "]\n\n")

if input && mod.typedDictArgs {
fmt.Fprintf(w, "MYPY = False\n\n")
}

var hasTypes bool
for _, t := range mod.types {
if t.IsOverlay {
Expand Down Expand Up @@ -2707,7 +2711,17 @@ func (mod *modContext) genDictType(w io.Writer, name, comment string, properties

indent := " "
name = pythonCase(name)
fmt.Fprintf(w, "class %sDict(TypedDict):\n", name)

// Running mypy gets very slow when there are a lot of TypedDicts.
// https://github.com/python/mypy/issues/17231
// For now we only use the TypedDict types when using a typechecker
// other than mypy. For mypy we define the XXXArgsDict class as an alias
// to the type `Mapping[str, Any]`.
fmt.Fprintf(w, "if not MYPY:\n")
fmt.Fprintf(w, "%sclass %sDict(TypedDict):\n", indent, name)

indent += " "

if comment != "" {
printComment(w, comment, indent)
}
Expand All @@ -2721,6 +2735,10 @@ func (mod *modContext) genDictType(w io.Writer, name, comment string, properties
}
}

indent = " "
fmt.Fprintf(w, "elif False:\n")
fmt.Fprintf(w, "%s%sDict: TypeAlias = Mapping[str, Any]\n", indent, name)

fmt.Fprintf(w, "\n")
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
if sys.version_info >= (3, 11):
from typing import NotRequired, TypedDict
from typing import NotRequired, TypedDict, TypeAlias
else:
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired, TypedDict, TypeAlias
from . import _utilities
import pulumi_kubernetes

Expand All @@ -22,10 +22,15 @@
'MyTypeArgsDict',
]

class MyTypeDict(TypedDict):
my_prop: str
external_prop: NotRequired['pulumi_kubernetes.core.v1.PodArgs']
my_other_prop: NotRequired[float]
MYPY = False

if not MYPY:
class MyTypeDict(TypedDict):
my_prop: str
external_prop: NotRequired['pulumi_kubernetes.core.v1.PodArgs']
my_other_prop: NotRequired[float]
elif False:
MyTypeDict: TypeAlias = Mapping[str, Any]

@pulumi.input_type
class MyType:
Expand Down Expand Up @@ -67,10 +72,13 @@ def my_other_prop(self, value: Optional[float]):
pulumi.set(self, "my_other_prop", value)


class MyTypeArgsDict(TypedDict):
my_prop: pulumi.Input[str]
external_prop: NotRequired[pulumi.Input['pulumi_kubernetes.core.v1.PodArgs']]
my_other_prop: NotRequired[pulumi.Input[float]]
if not MYPY:
class MyTypeArgsDict(TypedDict):
my_prop: pulumi.Input[str]
external_prop: NotRequired[pulumi.Input['pulumi_kubernetes.core.v1.PodArgs']]
my_other_prop: NotRequired[pulumi.Input[float]]
elif False:
MyTypeArgsDict: TypeAlias = Mapping[str, Any]

@pulumi.input_type
class MyTypeArgs:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
if sys.version_info >= (3, 11):
from typing import NotRequired, TypedDict
from typing import NotRequired, TypedDict, TypeAlias
else:
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired, TypedDict, TypeAlias
from . import _utilities
from ._inputs import *
import pulumi_kubernetes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
if sys.version_info >= (3, 11):
from typing import NotRequired, TypedDict
from typing import NotRequired, TypedDict, TypeAlias
else:
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired, TypedDict, TypeAlias
from . import _utilities
from . import outputs
from ._inputs import *
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
if sys.version_info >= (3, 11):
from typing import NotRequired, TypedDict
from typing import NotRequired, TypedDict, TypeAlias
else:
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired, TypedDict, TypeAlias
from . import _utilities

__all__ = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@
import pulumi.runtime
from typing import Any, Mapping, Optional, Sequence, Union, overload
if sys.version_info >= (3, 11):
from typing import NotRequired, TypedDict
from typing import NotRequired, TypedDict, TypeAlias
else:
from typing_extensions import NotRequired, TypedDict
from typing_extensions import NotRequired, TypedDict, TypeAlias
from . import _utilities

__all__ = ['ProviderArgs', 'Provider']
Expand Down

0 comments on commit 18d2079

Please sign in to comment.