Skip to content

Commit

Permalink
Make pyright happy
Browse files Browse the repository at this point in the history
  • Loading branch information
gvanrossum committed Apr 22, 2024
1 parent b90faa7 commit 02e28e4
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
2 changes: 1 addition & 1 deletion python/examples/drawing/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import schema as drawing
from render import render_drawing

from typechat import Success, Failure, TypeChatJsonTranslator, TypeChatValidator, PromptSection, create_language_model, process_requests
from typechat import Success, Failure, TypeChatJsonTranslator, TypeChatValidator, create_language_model, process_requests


async def main(file_path: str | None):
Expand Down
20 changes: 11 additions & 9 deletions python/examples/drawing/render.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import tkinter as tk

from schema import Drawing, Box, Ellipse, Arrow

# Map line style to dash patterns
dash_pattern = {
'solid': None,
'solid': '',
'dashed': (4, 4), # 4 pixels drawn, 4 pixels space
'dotted': (1, 1) # 1 pixel drawn, 1 pixel space
}

def render_drawing(drawing):
def render_drawing(drawing: Drawing):
# Create a new Tkinter window
window = tk.Tk()
window.title("Drawing")
Expand All @@ -18,7 +20,7 @@ def render_drawing(drawing):
canvas.pack(padx=10, pady=10) # Adds 10 pixels of padding on all sides

# Function to draw a box with text if provided
def draw_box(box):
def draw_box(box: Box):
x1, y1 = box['x'], box['y']
x2, y2 = x1 + box['width'], y1 + box['height']
fill = box['style'].get('fill_color', '') if box['style'] else ''
Expand All @@ -27,7 +29,7 @@ def draw_box(box):
canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, text=box['text'], fill='black')

# Function to draw an ellipse with text if provided
def draw_ellipse(ellipse):
def draw_ellipse(ellipse: Ellipse):
x1, y1 = ellipse['x'], ellipse['y']
x2, y2 = x1 + ellipse['width'], y1 + ellipse['height']
fill = ellipse['style'].get('fill_color', '') if ellipse['style'] else ''
Expand All @@ -36,7 +38,7 @@ def draw_ellipse(ellipse):
canvas.create_text((x1 + x2) / 2, (y1 + y2) / 2, text=ellipse['text'], fill='black')

# Function to draw an arrow
def draw_arrow(arrow):
def draw_arrow(arrow: Arrow):
x1, y1 = arrow['start_x'], arrow['start_y']
x2, y2 = arrow['end_x'], arrow['end_y']
line_style = (arrow['style'].get('line_style', 'solid') # Default line style
Expand All @@ -62,13 +64,13 @@ def draw_arrow(arrow):

# Example usage:
if __name__ == '__main__':
drawing = {
drawing: Drawing = {
'items': [
{'type': 'Box', 'x': 50, 'y': 50, 'width': 100, 'height': 100, 'text': 'Hello', 'style': None},
{'type': 'Ellipse', 'x': 200, 'y': 50, 'width': 150, 'height': 100, 'text': 'World', 'style': {'fill_color': 'lightblue'}},
{'type': 'Ellipse', 'x': 200, 'y': 50, 'width': 150, 'height': 100, 'text': 'World', 'style': {'type': 'Style', 'fill_color': 'lightblue'}},
{'type': 'Arrow', 'start_x': 50, 'start_y': 200, 'end_x': 150, 'end_y': 200,
'style': {'type': 'Style', 'line_style': 'dashed'}, 'head_size': 10}
]
'style': {'type': 'Style', 'line_style': 'dashed'}, 'head_size': 10},
],
}

render_drawing(drawing)
12 changes: 6 additions & 6 deletions python/examples/drawing/schema.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
from typing_extensions import Literal, TypedDict, Annotated, Doc, Optional
from typing_extensions import Literal, TypedDict, Annotated, Doc, NotRequired, Optional


class Style(TypedDict):
type: Literal["Style"]
corners: Annotated[Literal["rounded", "sharp"], Doc("Corner style of the drawing elements.")]
line_thickness: Annotated[Optional[int], Doc("Thickness of the lines.")]
line_color: Annotated[Optional[str], Doc("CSS-style color code for line color.")]
fill_color: Annotated[Optional[str], Doc("CSS-style color code for fill color.")]
line_style: Annotated[Optional[str], Doc("Style of the line: 'solid', 'dashed', 'dotted'.")]
corners: Annotated[NotRequired[Literal["rounded", "sharp"]], Doc("Corner style of the drawing elements.")]
line_thickness: Annotated[NotRequired[int], Doc("Thickness of the lines.")]
line_color: Annotated[NotRequired[str], Doc("CSS-style color code for line color.")]
fill_color: Annotated[NotRequired[str], Doc("CSS-style color code for fill color.")]
line_style: Annotated[NotRequired[str], Doc("Style of the line: 'solid', 'dashed', 'dotted'.")]


class Box(TypedDict):
Expand Down

0 comments on commit 02e28e4

Please sign in to comment.