Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions checks/type_hints/type_hints1.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
assert "count" in __annotations__, "count should have a type annotation"
assert __annotations__["count"] is int, "count should be annotated as int"
# checks/type_hints/type_hints1.py

import inspect

annotations = globals().get("__annotations__")
annotate = globals().get("__annotate__")
if annotations is None and callable(annotate):
# Python 3.14+ stores module annotations in a callable `__annotate__`
# instead of the `__annotations__` dict. Read them in VALUE format.
format_value = getattr(getattr(inspect, "Format", None), "VALUE", 1)
annotations = annotate(format_value)
annotations = annotations or {}
assert annotations.get("count") is int, "count should be annotated as int"
print("type_hints1 ✓")
Binary file modified docs-site/assets/logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 55 additions & 0 deletions docs/assets/branding/pythonlings-hero-source.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<!-- docs/assets/branding/pythonlings-hero-source.html -->
<!-- Source for pythonlings-hero.png. Regenerate from this directory with:
google-chrome --headless=new --hide-scrollbars --force-device-scale-factor=2 \
--window-size=1280,640 --virtual-time-budget=8000 \
--screenshot=pythonlings-hero.png "file://$PWD/pythonlings-hero-source.html" -->
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=JetBrains+Mono:wght@400;700;800&display=swap">
<style>
html, body { margin: 0; padding: 0; }
body {
width: 1280px; height: 640px; overflow: hidden;
background: #0B1117;
font-family: 'JetBrains Mono', monospace;
}
.wrap {
box-sizing: border-box;
width: 1280px; height: 640px;
position: relative;
display: flex; flex-direction: row; align-items: center; justify-content: center;
}
.col { display: flex; flex-direction: column; gap: 26px; }
.shell { display: flex; flex-direction: column; gap: 14px; font-size: 22px; }
.shell .prompt { color: #4ADE80; }
.shell .dim { color: #9AA7B4; }
.shell .cmd { color: #E2E8F0; }
.status { display: flex; gap: 28px; }
.status .pass { color: #4ADE80; }
.status .cur { color: #FFD43B; }
.status .count { color: #46566A; }
.wordmark { font-size: 88px; font-weight: 800; line-height: 1.1; letter-spacing: -0.03em; }
.wordmark .py { color: #5EA3D9; }
.wordmark .lings { color: #FFD43B; }
.wordmark .cursor { display: inline-block; width: 34px; height: 72px; background: #4ADE80; margin-left: 14px; }
.tagline { font-size: 24px; color: #8A97A5; }
.mascot { height: 340px; width: auto; position: absolute; right: 36px; top: 50%; transform: translateY(-50%); opacity: 0.95; }
</style>
</head>
<body>
<div class="wrap">
<div class="col">
<div class="shell">
<div><span class="prompt">$</span> <span class="dim">pip install pythonlings</span></div>
<div><span class="prompt">$</span> <span class="cmd">pythonlings</span></div>
<div class="status"><span class="pass">&#10003; variables1</span><span class="pass">&#10003; variables2</span><span class="cur">&#9679; variables3</span><span class="count">2/292</span></div>
</div>
<div class="wordmark"><span class="py">python</span><span class="lings">lings</span><span class="cursor"></span></div>
<div class="tagline">Learn Python by fixing tiny broken programs.</div>
</div>
<img class="mascot" src="pythonlings-mascot.png" alt="">
</div>
</body>
</html>
Binary file modified docs/assets/branding/pythonlings-hero.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/branding/pythonlings-logo-monochrome.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/branding/pythonlings-logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified docs/assets/branding/pythonlings-mascot.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
63 changes: 63 additions & 0 deletions tests/unit/test_type_hints1_check.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
# tests/unit/test_type_hints1_check.py

from pathlib import Path

from pythonlings.core.exercise import Exercise
from pythonlings.core.runner import run

ROOT = Path(__file__).resolve().parent.parent.parent
CHECK_SRC = (ROOT / "checks" / "type_hints" / "type_hints1.py").read_text(
encoding="utf-8"
)


def _run_case(tmp_path: Path, exercise_src: str) -> object:
ex_path = tmp_path / "ex.py"
check_path = tmp_path / "check.py"
ex_path.write_text(exercise_src, encoding="utf-8")
check_path.write_text(CHECK_SRC, encoding="utf-8")
exercise = Exercise(
name="type_hints1",
path=ex_path,
check_path=check_path,
topic="type_hints",
hint="",
)
return run(exercise)


def test_correct_annotation_passes(tmp_path: Path) -> None:
result = _run_case(tmp_path, "count: int = 0\n")
assert result.passed is True
assert "type_hints1 ✓" in result.stdout


def test_wrong_type_fails_with_clear_message(tmp_path: Path) -> None:
result = _run_case(tmp_path, "count: str = ''\n")
assert result.passed is False
assert "AssertionError" in result.stderr
assert "count should be annotated as int" in result.stderr
assert "NameError" not in result.stderr


def test_missing_annotation_fails_with_clear_message(tmp_path: Path) -> None:
result = _run_case(tmp_path, "total = 0\n")
assert result.passed is False
assert "AssertionError" in result.stderr
assert "count should be annotated as int" in result.stderr
assert "NameError" not in result.stderr


def test_none_annotation_hook_fails_with_clear_message(tmp_path: Path) -> None:
result = _run_case(tmp_path, "__annotate__ = None\n")
assert result.passed is False
assert "AssertionError" in result.stderr
assert "count should be annotated as int" in result.stderr
assert "TypeError" not in result.stderr


def test_callable_annotation_hook_passes(tmp_path: Path) -> None:
exercise_src = "def __annotate__(format_value):\n return {'count': int}\n"
result = _run_case(tmp_path, exercise_src)
assert result.passed is True
assert "type_hints1 ✓" in result.stdout
Loading