8000 gh-82129: Improve annotations for make_dataclass() by JelleZijlstra · Pull Request #133406 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-82129: Improve annotations for make_dataclass() #133406

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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 14 commits into from
May 5, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
WIP
  • Loading branch information
sobolevn committed Oct 15, 2024
commit 3b26e36e55f85a73866fa0ae04cb190ea6be01de
6 changes: 3 additions & 3 deletions Lib/dataclasses.py
Original file line number Diff line number Diff line change
Expand Up @@ -1620,13 +1620,13 @@ def annotate_method(format):
for ann, t in annotations.items()
}

from typing import Any, _convert_to_source
from typing import Any
ann_dict = {
ann: Any if t is _ANY_MARKER else t
for ann, t in annotations.items()
}
if format == annotationlib.Format.SOURCE:
return _convert_to_source(ann_dict)
if format == annotationlib.Format.STRING:
return annotationlib.annotations_to_string(ann_dict)
return ann_dict

# Update 'ns' with the user-supplied namespace plus our calculated values.
Expand Down
11 changes: 6 additions & 5 deletions Lib/test/test_dataclasses/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import types
import weakref
import traceback
import sys
import unittest
from unittest.mock import Mock
from typing import ClassVar, Any, List, Union, Tuple, Dict, Generic, TypeVar, Optional, Protocol, DefaultDict
Expand Down Expand Up @@ -4247,16 +4248,16 @@ def test_no_types_get_annotations(self):
)
self.assertEqual(
annotationlib.get_annotations(
C, format=annotationlib.Format.SOURCE),
C, format=annotationlib.Format.STRING),
{'x': 'typing.Any', 'y': 'int', 'z': 'typing.Any'},
)

def test_no_types_no_typing_import(self):
import sys

C = make_dataclass('C', ['x', ('y', int)])

with import_helper.CleanImport('typing'):
self.assertNotIn('typing', sys.modules)
C = make_dataclass('C', ['x', ('y', int)])

self.assertNotIn('typing', sys.modules)
self.assertEqual(
annotationlib.get_annotations(
C, format=annotationlib.Format.FORWARDREF),
Expand Down
Loading
0