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

Make untyped decorator its own code#19911

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes fromall commits
Commits
Show all changes
13 commits
Select commitHold shift + click to select a range
2eae039
Update errorcodes.py: UNTYPED_DECORATOR
wyattscarpenterSep 22, 2025
021fc0a
Update messages.py: UNTYPED_DECORATOR
wyattscarpenterSep 22, 2025
6b0f0d8
black
wyattscarpenterSep 22, 2025
a586b8e
docu
wyattscarpenterSep 23, 2025
3282eb1
enable sub_code_of=MISC for backwards compat reason
wyattscarpenterSep 23, 2025
3456a8b
Apply suggestion from @wyattscarpenter
wyattscarpenterSep 23, 2025
19d9d40
Update error_code_list2.rst
wyattscarpenterSep 23, 2025
8b62963
add tests
wyattscarpenterSep 24, 2025
78af836
Update docs/source/error_code_list2.rst: fix heading underline
wyattscarpenterSep 24, 2025
10ffd2e
fix the fix
wyattscarpenterSep 24, 2025
93ed8dd
make untyped-decorator not a sub cose of misc, per suggestion
wyattscarpenterSep 25, 2025
263b93f
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot]Sep 25, 2025
ed20c35
actually this last thing should test sonething different
wyattscarpenterSep 25, 2025
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
23 changes: 23 additions & 0 deletionsdocs/source/error_code_list2.rst
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -676,3 +676,26 @@ Example:
print("red")
case _:
print("other")

.. _code-untyped-decorator:

Error if an untyped decorator makes a typed function effectively untyped [untyped-decorator]
--------------------------------------------------------------------------------------------

If enabled with :option:`--disallow-untyped-decorators <mypy --disallow-untyped-decorators>`
mypy generates an error if a typed function is wrapped by an untyped decorator
(as this would effectively remove the benefits of typing the function).

Example:

.. code-block:: python

def printing_decorator(func):
def wrapper(*args, **kwds):
print("Calling", func)
return func(*args, **kwds)
return wrapper
# A decorated function.
@printing_decorator # E: Untyped decorator makes function "add_forty_two" untyped [untyped-decorator]
def add_forty_two(value: int) -> int:
return value + 42
4 changes: 4 additions & 0 deletionsmypy/errorcodes.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -302,6 +302,10 @@ def __hash__(self) -> int:
sub_code_of=MISC,
)

UNTYPED_DECORATOR: Final = ErrorCode(
"untyped-decorator", "Error if an untyped decorator makes a typed function untyped", "General"
)

NARROWED_TYPE_NOT_SUBTYPE: Final = ErrorCode(
"narrowed-type-not-subtype",
"Warn if a TypeIs function's narrowed type is not a subtype of the original type",
Expand Down
6 changes: 5 additions & 1 deletionmypy/messages.py
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -2008,7 +2008,11 @@ def untyped_decorated_function(self, typ: Type, context: Context) -> None:
)

def typed_function_untyped_decorator(self, func_name: str, context: Context) -> None:
self.fail(f'Untyped decorator makes function "{func_name}" untyped', context)
self.fail(
f'Untyped decorator makes function "{func_name}" untyped',
context,
code=codes.UNTYPED_DECORATOR,
)

def bad_proto_variance(
self, actual: int, tvar_name: str, expected: int, context: Context
Expand Down
15 changes: 15 additions & 0 deletionstest-data/unit/check-errorcodes.test
View file
Open in desktop
Original file line numberDiff line numberDiff line change
Expand Up@@ -394,6 +394,21 @@ def f() -> None:
def g():
pass

[case testErrorCodeUntypedDecorator]
# flags: --disallow-untyped-decorators --warn-unused-ignores
def d(f): return f

@d # E: Untyped decorator makes function "x" untyped [untyped-decorator]
def x() -> int: return 1
@d # type: ignore
def y() -> int: return 2
@d # type: ignore[untyped-decorator]
def best() -> int: return 3
@d # type: ignore[misc] # E: Unused "type: ignore" comment [unused-ignore] \
# E: Untyped decorator makes function "z" untyped [untyped-decorator] \
# N: Error code "untyped-decorator" not covered by "type: ignore" comment
def z() -> int: return 4

[case testErrorCodeIndexing]
from typing import Dict
x: Dict[int, int]
Expand Down
Loading

[8]ページ先頭

©2009-2025 Movatter.jp