Skip to content

Commit 2672786

Browse files
committed
Prefer Union[T | None] over Optional[T]
1 parent d99b819 commit 2672786

File tree

3 files changed

+27
-27
lines changed

3 files changed

+27
-27
lines changed

scripts/generate-schema/gen-python.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,9 +129,9 @@ function generateNode(node: Node, writer: Writer) {
129129

130130
for (const { key, required, description, value } of sortedProperties) {
131131
w(` ${key}: `);
132-
if (!required) w("Optional[");
132+
if (!required) w("Union[");
133133
generateNode(value, writer);
134-
if (!required) w("] = None");
134+
if (!required) w(" | None] = None");
135135
wn("");
136136
if (description) {
137137
wn(` """${description}"""`);
@@ -163,11 +163,11 @@ function generateNode(node: Node, writer: Writer) {
163163

164164
for (const { key, required, description, value } of sortedProperties) {
165165
d(` ${key}: `);
166-
if (!required) d("Optional[");
166+
if (!required) d("Union[");
167167
!isComplexType(value)
168168
? generateNode(value, depWriter)
169169
: d(value.name ?? value.type);
170-
if (!required) d("] = None");
170+
if (!required) d(" | None] = None");
171171
dn("");
172172
if (description) {
173173
dn(` """${description}"""`);
Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# DO NOT EDIT: This file is auto-generated by generate-schema/index.ts
22
from enum import Enum
3-
from typing import Optional, Union
3+
from typing import Union
44
import msgspec
55

66
class Size(msgspec.Struct, omit_defaults=True):
@@ -12,13 +12,13 @@ class Size(msgspec.Struct, omit_defaults=True):
1212
class WebViewContentUrl(msgspec.Struct, kw_only=True, omit_defaults=True):
1313
url: str
1414
"""Url to load in the webview. Note: Don't use data URLs here, as they are not supported. Use the `html` field instead."""
15-
headers: Optional[dict[str, str]] = None
15+
headers: Union[dict[str, str] | None] = None
1616
"""Optional headers to send with the request."""
1717

1818
class WebViewContentHtml(msgspec.Struct, kw_only=True, omit_defaults=True):
1919
html: str
2020
"""Html to load in the webview."""
21-
origin: Optional[str] = None
21+
origin: Union[str | None] = None
2222
"""What to set as the origin of the webview when loading html."""
2323

2424
WebViewContent = Union[WebViewContentUrl, WebViewContentHtml]
@@ -36,36 +36,36 @@ class WebViewOptions(msgspec.Struct, omit_defaults=True):
3636
"""
3737
title: str
3838
"""Sets the title of the window."""
39-
acceptFirstMouse: Optional[bool] = None
39+
acceptFirstMouse: Union[bool | None] = None
4040
"""Sets whether clicking an inactive window also clicks through to the webview. Default is false."""
41-
autoplay: Optional[bool] = None
41+
autoplay: Union[bool | None] = None
4242
"""When true, all media can be played without user interaction. Default is false."""
43-
clipboard: Optional[bool] = None
43+
clipboard: Union[bool | None] = None
4444
"""Enables clipboard access for the page rendered on Linux and Windows.
4545
4646
macOS doesn’t provide such method and is always enabled by default. But your app will still need to add menu item accelerators to use the clipboard shortcuts."""
47-
decorations: Optional[bool] = None
47+
decorations: Union[bool | None] = None
4848
"""When true, the window will have a border, a title bar, etc. Default is true."""
49-
devtools: Optional[bool] = None
49+
devtools: Union[bool | None] = None
5050
"""Enable or disable webview devtools.
5151
5252
Note this only enables devtools to the webview. To open it, you can call `webview.open_devtools()`, or right click the page and open it from the context menu."""
53-
focused: Optional[bool] = None
53+
focused: Union[bool | None] = None
5454
"""Sets whether the webview should be focused when created. Default is false."""
55-
incognito: Optional[bool] = None
55+
incognito: Union[bool | None] = None
5656
"""Run the WebView with incognito mode. Note that WebContext will be ingored if incognito is enabled.
5757
5858
Platform-specific: - Windows: Requires WebView2 Runtime version 101.0.1210.39 or higher, does nothing on older versions, see https://learn.microsoft.com/en-us/microsoft-edge/webview2/release-notes/archive?tabs=dotnetcsharp#10121039"""
59-
initializationScript: Optional[str] = None
59+
initializationScript: Union[str | None] = None
6060
"""Run JavaScript code when loading new pages. When the webview loads a new page, this code will be executed. It is guaranteed that the code is executed before window.onload."""
61-
ipc: Optional[bool] = None
61+
ipc: Union[bool | None] = None
6262
"""Sets whether host should be able to receive messages from the webview via `window.ipc.postMessage`."""
63-
load: Optional[WebViewContent] = None
63+
load: Union[WebViewContent | None] = None
6464
"""The content to load into the webview."""
65-
size: Optional[WindowSize] = None
65+
size: Union[WindowSize | None] = None
6666
"""The size of the window."""
67-
transparent: Optional[bool] = None
67+
transparent: Union[bool | None] = None
6868
"""Sets whether the window should be transparent."""
69-
userAgent: Optional[str] = None
69+
userAgent: Union[str | None] = None
7070
"""Sets the user agent to use when loading pages."""
7171

src/clients/python/schemas/WebViewRequest.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# DO NOT EDIT: This file is auto-generated by generate-schema/index.ts
2-
from typing import Optional, Union
2+
from typing import Union
33
import msgspec
44

55
class Size(msgspec.Struct, omit_defaults=True):
@@ -45,7 +45,7 @@ class OpenDevToolsRequest(msgspec.Struct, tag_field="$type", tag="openDevTools")
4545
class GetSizeRequest(msgspec.Struct, tag_field="$type", tag="getSize"):
4646
id: str
4747
"""The id of the request."""
48-
include_decorations: Optional[bool] = None
48+
include_decorations: Union[bool | None] = None
4949
"""Whether to include the title bar and borders in the size measurement."""
5050

5151
class SetSizeRequest(msgspec.Struct, tag_field="$type", tag="setSize"):
@@ -57,35 +57,35 @@ class SetSizeRequest(msgspec.Struct, tag_field="$type", tag="setSize"):
5757
class FullscreenRequest(msgspec.Struct, tag_field="$type", tag="fullscreen"):
5858
id: str
5959
"""The id of the request."""
60-
fullscreen: Optional[bool] = None
60+
fullscreen: Union[bool | None] = None
6161
"""Whether to enter fullscreen mode. If left unspecified, the window will enter fullscreen mode if it is not already in fullscreen mode or exit fullscreen mode if it is currently in fullscreen mode."""
6262

6363
class MaximizeRequest(msgspec.Struct, tag_field="$type", tag="maximize"):
6464
id: str
6565
"""The id of the request."""
66-
maximized: Optional[bool] = None
66+
maximized: Union[bool | None] = None
6767
"""Whether to maximize the window. If left unspecified, the window will be maximized if it is not already maximized or restored if it was previously maximized."""
6868

6969
class MinimizeRequest(msgspec.Struct, tag_field="$type", tag="minimize"):
7070
id: str
7171
"""The id of the request."""
72-
minimized: Optional[bool] = None
72+
minimized: Union[bool | None] = None
7373
"""Whether to minimize the window. If left unspecified, the window will be minimized if it is not already minimized or restored if it was previously minimized."""
7474

7575
class LoadHtmlRequest(msgspec.Struct, tag_field="$type", tag="loadHtml"):
7676
html: str
7777
"""HTML to set as the content of the webview."""
7878
id: str
7979
"""The id of the request."""
80-
origin: Optional[str] = None
80+
origin: Union[str | None] = None
8181
"""What to set as the origin of the webview when loading html. If not specified, the origin will be set to the value of the `origin` field when the webview was created."""
8282

8383
class LoadUrlRequest(msgspec.Struct, tag_field="$type", tag="loadUrl"):
8484
id: str
8585
"""The id of the request."""
8686
url: str
8787
"""URL to load in the webview."""
88-
headers: Optional[dict[str, str]] = None
88+
headers: Union[dict[str, str] | None] = None
8989
"""Optional headers to send with the request."""
9090

9191
WebViewRequest = Union[GetVersionRequest, EvalRequest, SetTitleRequest, GetTitleRequest, SetVisibilityRequest, IsVisibleRequest, OpenDevToolsRequest, GetSizeRequest, SetSizeRequest, FullscreenRequest, MaximizeRequest, MinimizeRequest, LoadHtmlRequest, LoadUrlRequest]

0 commit comments

Comments
 (0)