From 2672786d860a3e28bd46add3e74eef5fe96fbe07 Mon Sep 17 00:00:00 2001 From: Justin Bennett Date: Thu, 6 Feb 2025 22:37:19 -0500 Subject: [PATCH] Prefer Union[T | None] over Optional[T] --- scripts/generate-schema/gen-python.ts | 8 ++--- src/clients/python/schemas/WebViewOptions.py | 32 ++++++++++---------- src/clients/python/schemas/WebViewRequest.py | 14 ++++----- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/scripts/generate-schema/gen-python.ts b/scripts/generate-schema/gen-python.ts index 897b94c..4350bfa 100644 --- a/scripts/generate-schema/gen-python.ts +++ b/scripts/generate-schema/gen-python.ts @@ -129,9 +129,9 @@ function generateNode(node: Node, writer: Writer) { for (const { key, required, description, value } of sortedProperties) { w(` ${key}: `); - if (!required) w("Optional["); + if (!required) w("Union["); generateNode(value, writer); - if (!required) w("] = None"); + if (!required) w(" | None] = None"); wn(""); if (description) { wn(` """${description}"""`); @@ -163,11 +163,11 @@ function generateNode(node: Node, writer: Writer) { for (const { key, required, description, value } of sortedProperties) { d(` ${key}: `); - if (!required) d("Optional["); + if (!required) d("Union["); !isComplexType(value) ? generateNode(value, depWriter) : d(value.name ?? value.type); - if (!required) d("] = None"); + if (!required) d(" | None] = None"); dn(""); if (description) { dn(` """${description}"""`); diff --git a/src/clients/python/schemas/WebViewOptions.py b/src/clients/python/schemas/WebViewOptions.py index b20329c..fd3e061 100644 --- a/src/clients/python/schemas/WebViewOptions.py +++ b/src/clients/python/schemas/WebViewOptions.py @@ -1,6 +1,6 @@ # DO NOT EDIT: This file is auto-generated by generate-schema/index.ts from enum import Enum -from typing import Optional, Union +from typing import Union import msgspec class Size(msgspec.Struct, omit_defaults=True): @@ -12,13 +12,13 @@ class Size(msgspec.Struct, omit_defaults=True): class WebViewContentUrl(msgspec.Struct, kw_only=True, omit_defaults=True): url: str """Url to load in the webview. Note: Don't use data URLs here, as they are not supported. Use the `html` field instead.""" - headers: Optional[dict[str, str]] = None + headers: Union[dict[str, str] | None] = None """Optional headers to send with the request.""" class WebViewContentHtml(msgspec.Struct, kw_only=True, omit_defaults=True): html: str """Html to load in the webview.""" - origin: Optional[str] = None + origin: Union[str | None] = None """What to set as the origin of the webview when loading html.""" WebViewContent = Union[WebViewContentUrl, WebViewContentHtml] @@ -36,36 +36,36 @@ class WebViewOptions(msgspec.Struct, omit_defaults=True): """ title: str """Sets the title of the window.""" - acceptFirstMouse: Optional[bool] = None + acceptFirstMouse: Union[bool | None] = None """Sets whether clicking an inactive window also clicks through to the webview. Default is false.""" - autoplay: Optional[bool] = None + autoplay: Union[bool | None] = None """When true, all media can be played without user interaction. Default is false.""" - clipboard: Optional[bool] = None + clipboard: Union[bool | None] = None """Enables clipboard access for the page rendered on Linux and Windows. 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.""" - decorations: Optional[bool] = None + decorations: Union[bool | None] = None """When true, the window will have a border, a title bar, etc. Default is true.""" - devtools: Optional[bool] = None + devtools: Union[bool | None] = None """Enable or disable webview devtools. 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.""" - focused: Optional[bool] = None + focused: Union[bool | None] = None """Sets whether the webview should be focused when created. Default is false.""" - incognito: Optional[bool] = None + incognito: Union[bool | None] = None """Run the WebView with incognito mode. Note that WebContext will be ingored if incognito is enabled. 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""" - initializationScript: Optional[str] = None + initializationScript: Union[str | None] = None """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.""" - ipc: Optional[bool] = None + ipc: Union[bool | None] = None """Sets whether host should be able to receive messages from the webview via `window.ipc.postMessage`.""" - load: Optional[WebViewContent] = None + load: Union[WebViewContent | None] = None """The content to load into the webview.""" - size: Optional[WindowSize] = None + size: Union[WindowSize | None] = None """The size of the window.""" - transparent: Optional[bool] = None + transparent: Union[bool | None] = None """Sets whether the window should be transparent.""" - userAgent: Optional[str] = None + userAgent: Union[str | None] = None """Sets the user agent to use when loading pages.""" diff --git a/src/clients/python/schemas/WebViewRequest.py b/src/clients/python/schemas/WebViewRequest.py index 2260258..0da2606 100644 --- a/src/clients/python/schemas/WebViewRequest.py +++ b/src/clients/python/schemas/WebViewRequest.py @@ -1,5 +1,5 @@ # DO NOT EDIT: This file is auto-generated by generate-schema/index.ts -from typing import Optional, Union +from typing import Union import msgspec class Size(msgspec.Struct, omit_defaults=True): @@ -45,7 +45,7 @@ class OpenDevToolsRequest(msgspec.Struct, tag_field="$type", tag="openDevTools") class GetSizeRequest(msgspec.Struct, tag_field="$type", tag="getSize"): id: str """The id of the request.""" - include_decorations: Optional[bool] = None + include_decorations: Union[bool | None] = None """Whether to include the title bar and borders in the size measurement.""" class SetSizeRequest(msgspec.Struct, tag_field="$type", tag="setSize"): @@ -57,19 +57,19 @@ class SetSizeRequest(msgspec.Struct, tag_field="$type", tag="setSize"): class FullscreenRequest(msgspec.Struct, tag_field="$type", tag="fullscreen"): id: str """The id of the request.""" - fullscreen: Optional[bool] = None + fullscreen: Union[bool | None] = None """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.""" class MaximizeRequest(msgspec.Struct, tag_field="$type", tag="maximize"): id: str """The id of the request.""" - maximized: Optional[bool] = None + maximized: Union[bool | None] = None """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.""" class MinimizeRequest(msgspec.Struct, tag_field="$type", tag="minimize"): id: str """The id of the request.""" - minimized: Optional[bool] = None + minimized: Union[bool | None] = None """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.""" class LoadHtmlRequest(msgspec.Struct, tag_field="$type", tag="loadHtml"): @@ -77,7 +77,7 @@ class LoadHtmlRequest(msgspec.Struct, tag_field="$type", tag="loadHtml"): """HTML to set as the content of the webview.""" id: str """The id of the request.""" - origin: Optional[str] = None + origin: Union[str | None] = None """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.""" class LoadUrlRequest(msgspec.Struct, tag_field="$type", tag="loadUrl"): @@ -85,7 +85,7 @@ class LoadUrlRequest(msgspec.Struct, tag_field="$type", tag="loadUrl"): """The id of the request.""" url: str """URL to load in the webview.""" - headers: Optional[dict[str, str]] = None + headers: Union[dict[str, str] | None] = None """Optional headers to send with the request.""" WebViewRequest = Union[GetVersionRequest, EvalRequest, SetTitleRequest, GetTitleRequest, SetVisibilityRequest, IsVisibleRequest, OpenDevToolsRequest, GetSizeRequest, SetSizeRequest, FullscreenRequest, MaximizeRequest, MinimizeRequest, LoadHtmlRequest, LoadUrlRequest]