7
7
msgstr ""
8
8
"Project-Id-Version : Python 3.11\n "
9
9
"Report-Msgid-Bugs-To : \n "
10
- "POT-Creation-Date : 2022-06-03 00:13 +0000\n "
10
+ "POT-Creation-Date : 2022-12-25 00:16 +0000\n "
11
11
"PO-Revision-Date : YEAR-MO-DA HO:MI+ZONE\n "
12
12
"Last-Translator : FULL NAME <EMAIL@ADDRESS>\n "
13
13
"Language-Team : Chinese - TAIWAN (https://github.com/python/python-docs-zh- "
@@ -91,18 +91,26 @@ msgid ""
91
91
"three arguments, for example ``getattr(o, '__annotations__', None)``."
92
92
msgstr ""
93
93
94
- #: ../../howto/annotations.rst:62
94
+ #: ../../howto/annotations.rst:60
95
+ msgid ""
96
+ "Before Python 3.10, accessing ``__annotations__`` on a class that defines no "
97
+ "annotations but that has a parent class with annotations would return the "
98
+ "parent's ``__annotations__``. In Python 3.10 and newer, the child class's "
99
+ "annotations will be an empty dict instead."
100
+ msgstr ""
101
+
102
+ #: ../../howto/annotations.rst:68
95
103
msgid "Accessing The Annotations Dict Of An Object In Python 3.9 And Older"
96
104
msgstr ""
97
105
98
- #: ../../howto/annotations.rst:64
106
+ #: ../../howto/annotations.rst:70
99
107
msgid ""
100
108
"In Python 3.9 and older, accessing the annotations dict of an object is much "
101
109
"more complicated than in newer versions. The problem is a design flaw in "
102
110
"these older versions of Python, specifically to do with class annotations."
103
111
msgstr ""
104
112
105
- #: ../../howto/annotations.rst:69
113
+ #: ../../howto/annotations.rst:75
106
114
msgid ""
107
115
"Best practice for accessing the annotations dict of other objects--"
108
116
"functions, other callables, and modules--is the same as best practice for "
@@ -111,7 +119,7 @@ msgid ""
111
119
"``__annotations__`` attribute."
112
120
msgstr ""
113
121
114
- #: ../../howto/annotations.rst:76
122
+ #: ../../howto/annotations.rst:82
115
123
msgid ""
116
124
"Unfortunately, this isn't best practice for classes. The problem is that, "
117
125
"since ``__annotations__`` is optional on classes, and because classes can "
@@ -120,11 +128,11 @@ msgid ""
120
128
"annotations dict of a *base class.* As an example::"
121
129
msgstr ""
122
130
123
- #: ../../howto/annotations.rst:92
131
+ #: ../../howto/annotations.rst:98
124
132
msgid "This will print the annotations dict from ``Base``, not ``Derived``."
125
133
msgstr ""
126
134
127
- #: ../../howto/annotations.rst:95
135
+ #: ../../howto/annotations.rst:101
128
136
msgid ""
129
137
"Your code will have to have a separate code path if the object you're "
130
138
"examining is a class (``isinstance(o, type)``). In that case, best practice "
@@ -134,81 +142,81 @@ msgid ""
134
142
"practice is to call the ``get`` method on the class dict."
135
143
msgstr ""
136
144
137
- #: ../../howto/annotations.rst:103
145
+ #: ../../howto/annotations.rst:109
138
146
msgid ""
139
147
"To put it all together, here is some sample code that safely accesses the "
140
148
"``__annotations__`` attribute on an arbitrary object in Python 3.9 and "
141
149
"before::"
142
150
msgstr ""
143
151
144
- #: ../../howto/annotations.rst:112
152
+ #: ../../howto/annotations.rst:118
145
153
msgid ""
146
154
"After running this code, ``ann`` should be either a dictionary or ``None``. "
147
155
"You're encouraged to double-check the type of ``ann`` using :func:"
148
156
"`isinstance` before further examination."
149
157
msgstr ""
150
158
151
- #: ../../howto/annotations.rst:117
159
+ #: ../../howto/annotations.rst:123
152
160
msgid ""
153
161
"Note that some exotic or malformed type objects may not have a ``__dict__`` "
154
162
"attribute, so for extra safety you may also wish to use :func:`getattr` to "
155
163
"access ``__dict__``."
156
164
msgstr ""
157
165
158
- #: ../../howto/annotations.rst:123
166
+ #: ../../howto/annotations.rst:129
159
167
msgid "Manually Un-Stringizing Stringized Annotations"
160
168
msgstr ""
161
169
162
- #: ../../howto/annotations.rst:125
170
+ #: ../../howto/annotations.rst:131
163
171
msgid ""
164
172
"In situations where some annotations may be \" stringized\" , and you wish to "
165
173
"evaluate those strings to produce the Python values they represent, it "
166
174
"really is best to call :func:`inspect.get_annotations` to do this work for "
167
175
"you."
168
176
msgstr ""
169
177
170
- #: ../../howto/annotations.rst:131
178
+ #: ../../howto/annotations.rst:137
171
179
msgid ""
172
180
"If you're using Python 3.9 or older, or if for some reason you can't use :"
173
181
"func:`inspect.get_annotations`, you'll need to duplicate its logic. You're "
174
182
"encouraged to examine the implementation of :func:`inspect.get_annotations` "
175
183
"in the current Python version and follow a similar approach."
176
184
msgstr ""
177
185
178
- #: ../../howto/annotations.rst:137
186
+ #: ../../howto/annotations.rst:143
179
187
msgid ""
180
188
"In a nutshell, if you wish to evaluate a stringized annotation on an "
181
189
"arbitrary object ``o``:"
182
190
msgstr ""
183
191
184
- #: ../../howto/annotations.rst:140
192
+ #: ../../howto/annotations.rst:146
185
193
msgid ""
186
194
"If ``o`` is a module, use ``o.__dict__`` as the ``globals`` when calling :"
187
195
"func:`eval`."
188
196
msgstr ""
189
197
190
- #: ../../howto/annotations.rst:142
198
+ #: ../../howto/annotations.rst:148
191
199
msgid ""
192
200
"If ``o`` is a class, use ``sys.modules[o.__module__].__dict__`` as the "
193
201
"``globals``, and ``dict(vars(o))`` as the ``locals``, when calling :func:"
194
202
"`eval`."
195
203
msgstr ""
196
204
197
- #: ../../howto/annotations.rst:145
205
+ #: ../../howto/annotations.rst:151
198
206
msgid ""
199
207
"If ``o`` is a wrapped callable using :func:`functools.update_wrapper`, :func:"
200
208
"`functools.wraps`, or :func:`functools.partial`, iteratively unwrap it by "
201
209
"accessing either ``o.__wrapped__`` or ``o.func`` as appropriate, until you "
202
210
"have found the root unwrapped function."
203
211
msgstr ""
204
212
205
- #: ../../howto/annotations.rst:149
213
+ #: ../../howto/annotations.rst:155
206
214
msgid ""
207
215
"If ``o`` is a callable (but not a class), use ``o.__globals__`` as the "
208
216
"globals when calling :func:`eval`."
209
217
msgstr ""
210
218
211
- #: ../../howto/annotations.rst:152
219
+ #: ../../howto/annotations.rst:158
212
220
msgid ""
213
221
"However, not all string values used as annotations can be successfully "
214
222
"turned into Python values by :func:`eval`. String values could theoretically "
@@ -217,63 +225,63 @@ msgid ""
217
225
"be evaluated. For example:"
218
226
msgstr ""
219
227
220
- #: ../../howto/annotations.rst:159
228
+ #: ../../howto/annotations.rst:165
221
229
msgid ""
222
230
":pep:`604` union types using ``|``, before support for this was added to "
223
231
"Python 3.10."
224
232
msgstr ""
225
233
226
- #: ../../howto/annotations.rst:161
234
+ #: ../../howto/annotations.rst:167
227
235
msgid ""
228
236
"Definitions that aren't needed at runtime, only imported when :const:`typing."
229
237
"TYPE_CHECKING` is true."
230
238
msgstr ""
231
239
232
- #: ../../howto/annotations.rst:164
240
+ #: ../../howto/annotations.rst:170
233
241
msgid ""
234
242
"If :func:`eval` attempts to evaluate such values, it will fail and raise an "
235
243
"exception. So, when designing a library API that works with annotations, "
236
244
"it's recommended to only attempt to evaluate string values when explicitly "
237
245
"requested to by the caller."
238
246
msgstr ""
239
247
240
- #: ../../howto/annotations.rst:172
248
+ #: ../../howto/annotations.rst:178
241
249
msgid "Best Practices For ``__annotations__`` In Any Python Version"
242
250
msgstr ""
243
251
244
- #: ../../howto/annotations.rst:174
252
+ #: ../../howto/annotations.rst:180
245
253
msgid ""
246
254
"You should avoid assigning to the ``__annotations__`` member of objects "
247
255
"directly. Let Python manage setting ``__annotations__``."
248
256
msgstr ""
249
257
250
- #: ../../howto/annotations.rst:177
258
+ #: ../../howto/annotations.rst:183
251
259
msgid ""
252
260
"If you do assign directly to the ``__annotations__`` member of an object, "
253
261
"you should always set it to a ``dict`` object."
254
262
msgstr ""
255
263
256
- #: ../../howto/annotations.rst:180
264
+ #: ../../howto/annotations.rst:186
257
265
msgid ""
258
266
"If you directly access the ``__annotations__`` member of an object, you "
259
267
"should ensure that it's a dictionary before attempting to examine its "
260
268
"contents."
261
269
msgstr ""
262
270
263
- #: ../../howto/annotations.rst:184
271
+ #: ../../howto/annotations.rst:190
264
272
msgid "You should avoid modifying ``__annotations__`` dicts."
265
273
msgstr ""
266
274
267
- #: ../../howto/annotations.rst:186
275
+ #: ../../howto/annotations.rst:192
268
276
msgid ""
269
277
"You should avoid deleting the ``__annotations__`` attribute of an object."
270
278
msgstr ""
271
279
272
- #: ../../howto/annotations.rst:191
280
+ #: ../../howto/annotations.rst:197
273
281
msgid "``__annotations__`` Quirks"
274
282
msgstr ""
275
283
276
- #: ../../howto/annotations.rst:193
284
+ #: ../../howto/annotations.rst:199
277
285
msgid ""
278
286
"In all versions of Python 3, function objects lazy-create an annotations "
279
287
"dict if no annotations are defined on that object. You can delete the "
@@ -285,13 +293,13 @@ msgid ""
285
293
"guaranteed to always throw an ``AttributeError``."
286
294
msgstr ""
287
295
288
- #: ../../howto/annotations.rst:203
296
+ #: ../../howto/annotations.rst:209
289
297
msgid ""
290
298
"Everything in the above paragraph also applies to class and module objects "
291
299
"in Python 3.10 and newer."
292
300
msgstr ""
293
301
294
- #: ../../howto/annotations.rst:206
302
+ #: ../../howto/annotations.rst:212
295
303
msgid ""
296
304
"In all versions of Python 3, you can set ``__annotations__`` on a function "
297
305
"object to ``None``. However, subsequently accessing the annotations on that "
@@ -302,15 +310,15 @@ msgid ""
302
310
"set."
303
311
msgstr ""
304
312
305
- #: ../../howto/annotations.rst:214
313
+ #: ../../howto/annotations.rst:220
306
314
msgid ""
307
315
"If Python stringizes your annotations for you (using ``from __future__ "
308
316
"import annotations``), and you specify a string as an annotation, the string "
309
317
"will itself be quoted. In effect the annotation is quoted *twice.* For "
310
318
"example::"
311
319
msgstr ""
312
320
313
- #: ../../howto/annotations.rst:225
321
+ #: ../../howto/annotations.rst:231
314
322
msgid ""
315
323
"This prints ``{'a': \" 'str'\" }``. This shouldn't really be considered a "
316
324
"\" quirk\" ; it's mentioned here simply because it might be surprising."
0 commit comments