Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit74be000

Browse files
hauntsaninjamiss-islington
authored andcommitted
Improve assert_type phrasing (pythonGH-104081)
I'd like to make the fact that this does nothing at runtimereally obvious, since I suspect this is unintuitive for users who areunfamiliar with static type checking.I thought of this because ofhttps://discuss.python.org/t/add-arg-check-type-to-types/26384wherein I'm skeptical that the user really did want `assert_type`.(cherry picked from commit82ba6ce)Co-authored-by: Shantanu <12621235+hauntsaninja@users.noreply.github.com>
1 parentb07bae6 commit74be000

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

‎Doc/library/typing.rst‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2438,15 +2438,16 @@ Functions and decorators
24382438

24392439
Ask a static type checker to confirm that *val* has an inferred type of *typ*.
24402440

2441-
When the type checker encounters a call to ``assert_type()``, it
2441+
At runtime this does nothing: it returns the first argument unchanged with no
2442+
checks or side effects, no matter the actual type of the argument.
2443+
2444+
When a static type checker encounters a call to ``assert_type()``, it
24422445
emits an error if the value is not of the specified type::
24432446

24442447
def greet(name: str) -> None:
24452448
assert_type(name, str) # OK, inferred type of `name` is `str`
24462449
assert_type(name, int) # type checker error
24472450

2448-
At runtime this returns the first argument unchanged with no side effects.
2449-
24502451
This function is useful for ensuring the type checker's understanding of a
24512452
script is in line with the developer's intentions::
24522453

‎Lib/typing.py‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2250,15 +2250,16 @@ def cast(typ, val):
22502250
defassert_type(val,typ,/):
22512251
"""Ask a static type checker to confirm that the value is of the given type.
22522252
2253-
When the type checker encounters a call to assert_type(), it
2253+
At runtime this does nothing: it returns the first argument unchanged with no
2254+
checks or side effects, no matter the actual type of the argument.
2255+
2256+
When a static type checker encounters a call to assert_type(), it
22542257
emits an error if the value is not of the specified type::
22552258
22562259
def greet(name: str) -> None:
22572260
assert_type(name, str) # ok
22582261
assert_type(name, int) # type checker error
22592262
2260-
At runtime this returns the first argument unchanged and otherwise
2261-
does nothing.
22622263
"""
22632264
returnval
22642265

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp