Skip to content

Commit 761749c

Browse files
authored
[playground] New default program (#17277)
## Summary This PR proposes to change the default example program in the playground. I realize that this is somewhat underwhelming, but I found it rather difficult to come up with something that circumvented missing support for overloads/generics/self-type, while still looking like (easy!) code that someone might actually write, and demonstrating some Red Knot features. One thing that I wanted to capture was the experience of adding type constraints to an untyped program. And I wanted something that could be executed in the Playground once all errors are fixed. Happy for any suggestions on what we could do instead. I had a lot of different ideas, but always ran into one or another limitation. So I guess we can also iterate on this as we add more features to Red Knot. Try it here: https://playknot.ruff.rs/8e3a96af-f35d-4488-840a-2abee6c0512d ```py from typing import Literal type Style = Literal["italic", "bold", "underline"] # Add parameter annotations `line: str, word: str, style: Style` and a return # type annotation `-> str` to see if you can find the mistakes in this program. def with_style(line, word, style): if style == "italic": return line.replace(word, f"*{word}*") elif style == "bold": return line.replace(word, f"__{word}__") position = line.find(word) output = line + "\n" output += " " * position output += "-" * len(word) print(with_style("Red Knot is a fast type checker for Python.", "fast", "underlined")) ``` closes #17267
1 parent 3657f79 commit 761749c

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

playground/knot/src/Playground.tsx

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,9 +205,31 @@ export const DEFAULT_SETTINGS = JSON.stringify(
205205
4,
206206
);
207207

208+
const DEFAULT_PROGRAM = `from typing import Literal
209+
210+
type Style = Literal["italic", "bold", "underline"]
211+
212+
# Add parameter annotations \`line: str, word: str, style: Style\` and a return
213+
# type annotation \`-> str\` to see if you can find the mistakes in this program.
214+
215+
def with_style(line, word, style):
216+
if style == "italic":
217+
return line.replace(word, f"*{word}*")
218+
elif style == "bold":
219+
return line.replace(word, f"__{word}__")
220+
221+
position = line.find(word)
222+
output = line + "\\n"
223+
output += " " * position
224+
output += "-" * len(word)
225+
226+
227+
print(with_style("Red Knot is a fast type checker for Python.", "fast", "underlined"))
228+
`;
229+
208230
const DEFAULT_WORKSPACE = {
209231
files: {
210-
"main.py": "import os",
232+
"main.py": DEFAULT_PROGRAM,
211233
"knot.json": DEFAULT_SETTINGS,
212234
},
213235
current: "main.py",

0 commit comments

Comments
 (0)