Skip to content

Commit

Permalink
Prefer Union[T | None] over Optional[T]
Browse files Browse the repository at this point in the history
  • Loading branch information
zephraph committed Feb 7, 2025
1 parent abdc6f3 commit 11ec657
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
8 changes: 4 additions & 4 deletions scripts/generate-schema/gen-python.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}"""`);
Expand Down Expand Up @@ -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}"""`);
Expand Down
32 changes: 16 additions & 16 deletions src/clients/python/schemas/WebViewOptions.py
Original file line number Diff line number Diff line change
@@ -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):
Expand All @@ -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]
Expand All @@ -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."""

14 changes: 7 additions & 7 deletions src/clients/python/schemas/WebViewRequest.py
Original file line number Diff line number Diff line change
@@ -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):
Expand Down Expand Up @@ -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"):
Expand All @@ -57,35 +57,35 @@ 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"):
html: str
"""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"):
id: str
"""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]
Expand Down

0 comments on commit 11ec657

Please sign in to comment.