@@ -128,6 +128,11 @@ msgid ""
128128"practice in Python versions 3.10 and newer is to call :func:`getattr` with "
129129"three arguments, for example ``getattr(o, '__annotations__', None)``."
130130msgstr ""
131+ "しかし、それ以外の callable (例えば、 :func:`functools.partial` で作られたも"
132+ "の) は ``__annotations__`` 属性が定義されていない可能性があります。この属性が"
133+ "あるかどうか不明なオブジェクトの場合は、Python 3.10 以降では、 :func:"
134+ "`getattr` に3つの引数を渡して呼び出す (例えば ``getattr(o, "
135+ "'__annotations__', None)`` ) のを推奨します。"
131136
132137#: ../../howto/annotations.rst:65
133138msgid ""
@@ -136,17 +141,24 @@ msgid ""
136141"parent's ``__annotations__``. In Python 3.10 and newer, the child class's "
137142"annotations will be an empty dict instead."
138143msgstr ""
144+ "Python 3.10 より前のバージョンでは、親クラスにアノテーションがあるが自身はア"
145+ "ノテーションを定義していないようなクラスの場合、 ``__annotations__`` を取得す"
146+ "ると親クラスの ``__annotations__`` が返されます。3.10 以降では、代わりに空の"
147+ "辞書が返されます。"
139148
140149#: ../../howto/annotations.rst:73
141150msgid "Accessing The Annotations Dict Of An Object In Python 3.9 And Older"
142- msgstr ""
151+ msgstr "オブジェクトのアノテーション辞書へのアクセス (Python 3.9 以前) "
143152
144153#: ../../howto/annotations.rst:75
145154msgid ""
146155"In Python 3.9 and older, accessing the annotations dict of an object is much "
147156"more complicated than in newer versions. The problem is a design flaw in "
148157"these older versions of Python, specifically to do with class annotations."
149158msgstr ""
159+ "Python 3.9 以前では、オブジェクトのアノテーション辞書を取得するのはより複雑で"
160+ "す。原因は、3.9 以前のバージョンの設計上の欠陥 (具体的にはクラスのアノテー"
161+ "ション) です。"
150162
151163#: ../../howto/annotations.rst:80
152164msgid ""
@@ -156,6 +168,10 @@ msgid ""
156168"should use three-argument :func:`getattr` to access the object's "
157169"``__annotations__`` attribute."
158170msgstr ""
171+ "クラス以外のオブジェクト (関数や他の callable、モジュール) のアノテーションを"
172+ "取得する推奨の方法は、3.10と同じです。 :func:`inspect.get_annotations` ではな"
173+ "く、 :func:`getattr` に3つの引数を渡すことで ``__annotations__`` 属性を取得"
174+ "するのがいいです。"
159175
160176#: ../../howto/annotations.rst:87
161177msgid ""
@@ -165,6 +181,10 @@ msgid ""
165181"``__annotations__`` attribute of a class may inadvertently return the "
166182"annotations dict of a *base class.* As an example::"
167183msgstr ""
184+ "ただし、クラスについては推奨方法が異なります。なぜなら、クラスには "
185+ "``__annotations__`` は必須ではなく、属性はベースクラスから継承されるので、 "
186+ "``__annotations__`` 属性にアクセスするとベースクラスのアノテーション辞書を取"
187+ "得してしまうという問題があるからです。例えば::"
168188
169189#: ../../howto/annotations.rst:94
170190msgid ""
@@ -177,10 +197,19 @@ msgid ""
177197"\n"
178198"print(Derived.__annotations__)"
179199msgstr ""
200+ "class Base:\n"
201+ " a: int = 3\n"
202+ " b: str = 'abc'\n"
203+ "\n"
204+ "class Derived(Base):\n"
205+ " pass\n"
206+ "\n"
207+ "print(Derived.__annotations__)"
180208
181209#: ../../howto/annotations.rst:103
182210msgid "This will print the annotations dict from ``Base``, not ``Derived``."
183211msgstr ""
212+ "このコードでは ``Derived`` ではなく ``Base`` のアノテーションが表示されます。"
184213
185214#: ../../howto/annotations.rst:106
186215msgid ""
@@ -191,13 +220,21 @@ msgid ""
191220"dictionary. Since the class may or may not have annotations defined, best "
192221"practice is to call the :meth:`~dict.get` method on the class dict."
193222msgstr ""
223+ "クラス (``isinstance(o, type)``) のアノテーションを取得するためには、新たに分"
224+ "岐を追加する必要があります。推奨する方法では、 Python 3.9 以前の実装に依存し"
225+ "た方法をとります。クラスにアノテーションがあれば、 :attr:`~type.__dict__` の"
226+ "辞書に格納されるという実装になっています。クラスにはアノテーションがない場合"
227+ "もあるため、この辞書の :meth:`~dict.get` メソッドを使ってアノテーションを取得"
228+ "する、というのが推奨の方法です。"
194229
195230#: ../../howto/annotations.rst:114
196231msgid ""
197232"To put it all together, here is some sample code that safely accesses the "
198233"``__annotations__`` attribute on an arbitrary object in Python 3.9 and "
199234"before::"
200235msgstr ""
236+ "以上を踏まえると、Python 3.9 以前では、以下のサンプルコードで任意のオブジェク"
237+ "トの ``__annotations__`` 属性を安全に取得することができます::"
201238
202239#: ../../howto/annotations.rst:118
203240msgid ""
@@ -206,24 +243,34 @@ msgid ""
206243"else:\n"
207244" ann = getattr(o, '__annotations__', None)"
208245msgstr ""
246+ "if isinstance(o, type):\n"
247+ " ann = o.__dict__.get('__annotations__', None)\n"
248+ "else:\n"
249+ " ann = getattr(o, '__annotations__', None)"
209250
210251#: ../../howto/annotations.rst:123
211252msgid ""
212253"After running this code, ``ann`` should be either a dictionary or ``None``. "
213254"You're encouraged to double-check the type of ``ann`` using :func:"
214255"`isinstance` before further examination."
215256msgstr ""
257+ "このコードを実行すると、 ``ann`` の値は辞書か ``None`` となります。さらに "
258+ "``ann`` の値を調べるには、 :func:`isinstance` で型を再度確認することを推奨し"
259+ "ます。"
216260
217261#: ../../howto/annotations.rst:128
218262msgid ""
219263"Note that some exotic or malformed type objects may not have a :attr:`~type."
220264"__dict__` attribute, so for extra safety you may also wish to use :func:"
221265"`getattr` to access :attr:`!__dict__`."
222266msgstr ""
267+ "ただし、一部の特殊な型オブジェクトや不正な形式の型オブジェクトは :attr:"
268+ "`~type.__dict__` 属性がないこともあるため、さらに安全をとるには :func:"
269+ "`getattr` を使って :attr:`!__dict__` を取得したほうがいいです。"
223270
224271#: ../../howto/annotations.rst:134
225272msgid "Manually Un-Stringizing Stringized Annotations"
226- msgstr ""
273+ msgstr "文字列指定のアノテーションを手動で解決する方法 "
227274
228275#: ../../howto/annotations.rst:136
229276msgid ""
@@ -232,6 +279,8 @@ msgid ""
232279"really is best to call :func:`inspect.get_annotations` to do this work for "
233280"you."
234281msgstr ""
282+ "アノテーションは文字列として指定されている場合があり、それを評価して Python "
283+ "の値に変換するには、 :func:`inspect.get_annotations` を使うのが一番です。"
235284
236285#: ../../howto/annotations.rst:142
237286msgid ""
@@ -240,25 +289,34 @@ msgid ""
240289"encouraged to examine the implementation of :func:`inspect.get_annotations` "
241290"in the current Python version and follow a similar approach."
242291msgstr ""
292+ "Python 3.9 以前を使っているか、もしくは何らかの理由で :func:`inspect."
293+ "get_annotations` を使えない場合は、自分で実装する必要があります。現状の "
294+ "Python バージョンの :func:`inspect.get_annotations` の実装を確認して、同様の"
295+ "処理を実装することを推奨します。"
243296
244297#: ../../howto/annotations.rst:148
245298msgid ""
246299"In a nutshell, if you wish to evaluate a stringized annotation on an "
247300"arbitrary object ``o``:"
248301msgstr ""
302+ "すなわち、任意のオブジェクト ``o`` の文字列アノテーションを評価するには:"
249303
250304#: ../../howto/annotations.rst:151
251305msgid ""
252306"If ``o`` is a module, use ``o.__dict__`` as the ``globals`` when calling :"
253307"func:`eval`."
254308msgstr ""
309+ "``o`` がモジュールならば、 ``o.__dict__`` を ``globals`` として :func:`eval` "
310+ "を呼ぶ。"
255311
256312#: ../../howto/annotations.rst:153
257313msgid ""
258314"If ``o`` is a class, use ``sys.modules[o.__module__].__dict__`` as the "
259315"``globals``, and ``dict(vars(o))`` as the ``locals``, when calling :func:"
260316"`eval`."
261317msgstr ""
318+ "``o`` がクラスならば、 ``sys.modules[o.__module__].__dict__`` を ``globals`` "
319+ "とし、 ``dict(vars(o))`` を ``locals`` として :func:`eval` を呼ぶ。"
262320
263321#: ../../howto/annotations.rst:156
264322msgid ""
@@ -267,12 +325,17 @@ msgid ""
267325"accessing either ``o.__wrapped__`` or ``o.func`` as appropriate, until you "
268326"have found the root unwrapped function."
269327msgstr ""
328+ "``o`` が :func:`functools.update_wrapper` か :func:`functools.wraps` か :"
329+ "func:`functools.partial` でラップされた callable ならば、一番内部の関数に到達"
330+ "するまで、 ``o.__wrapped__`` か ``o.func`` のどちらかを繰り返し取得する。"
270331
271332#: ../../howto/annotations.rst:160
272333msgid ""
273334"If ``o`` is a callable (but not a class), use :attr:`o.__globals__ <function."
274335"__globals__>` as the globals when calling :func:`eval`."
275336msgstr ""
337+ "``o`` がクラス以外の callable であれば、 :attr:`o.__globals__ <function."
338+ "__globals__>` を globals として :func:`eval` を呼ぶ。"
276339
277340#: ../../howto/annotations.rst:164
278341msgid ""
@@ -282,18 +345,25 @@ msgid ""
282345"hints that require annotating with string values that specifically *can't* "
283346"be evaluated. For example:"
284347msgstr ""
348+ "しかし、必ずしもアノテーションの文字列が :func:`eval` で Python の値に変換で"
349+ "きるとは限りません。理論上どんな文字列も指定可能であり、変換できない文字列を"
350+ "型ヒントとして指定する必要がある場合が実際にあります。例えば:"
285351
286352#: ../../howto/annotations.rst:171
287353msgid ""
288354":pep:`604` union types using ``|``, before support for this was added to "
289355"Python 3.10."
290356msgstr ""
357+ "``|`` を使った :pep:`604` のユニオン型 (これが追加された Python 3.10 より前の"
358+ "バージョン)"
291359
292360#: ../../howto/annotations.rst:173
293361msgid ""
294362"Definitions that aren't needed at runtime, only imported when :const:`typing."
295363"TYPE_CHECKING` is true."
296364msgstr ""
365+ ":const:`typing.TYPE_CHECKING` が True の時だけインポートされ、実行時には必要"
366+ "ない型定義。"
297367
298368#: ../../howto/annotations.rst:176
299369msgid ""
@@ -302,49 +372,61 @@ msgid ""
302372"it's recommended to only attempt to evaluate string values when explicitly "
303373"requested to by the caller."
304374msgstr ""
375+ ":func:`eval` で上記のような値を評価しようとすると、失敗し例外が発生します。そ"
376+ "のため、アノテーションを扱うライブラリ API を作る際は、呼び出し側が明示的に要"
377+ "求したときのみ、文字列を評価することが推奨されます。"
305378
306379#: ../../howto/annotations.rst:184
307380msgid "Best Practices For ``__annotations__`` In Any Python Version"
308- msgstr ""
381+ msgstr "``__annotations__`` のベストプラクティス (全 Python バージョン共通) "
309382
310383#: ../../howto/annotations.rst:186
311384msgid ""
312385"You should avoid assigning to the ``__annotations__`` member of objects "
313386"directly. Let Python manage setting ``__annotations__``."
314387msgstr ""
388+ "オブジェクトの ``__annotations__`` 属性に直接代入することは避けるべきです。"
389+ "Python に任せましょう。 "
315390
316391#: ../../howto/annotations.rst:189
317392msgid ""
318393"If you do assign directly to the ``__annotations__`` member of an object, "
319394"you should always set it to a ``dict`` object."
320395msgstr ""
396+ "直接 ``__annotations__`` 属性に代入するなら、必ず ``dict`` オブジェクトを代入"
397+ "するべきです。"
321398
322399#: ../../howto/annotations.rst:192
323400msgid ""
324401"You should avoid accessing ``__annotations__`` directly on any object. "
325402"Instead, use :func:`annotationlib.get_annotations` (Python 3.14+) or :func:"
326403"`inspect.get_annotations` (Python 3.10+)."
327404msgstr ""
405+ "どういうオブジェクトかに関わらず、直接 ``__annotations__`` にアクセスするのは"
406+ "避けるべきです。Python 3.14 以降は :func:`annotationlib.get_annotations` 、 "
407+ "Python 3.10 以降は :func:`inspect.get_annotations` を使いましょう。"
328408
329409#: ../../howto/annotations.rst:196
330410msgid ""
331411"If you do directly access the ``__annotations__`` member of an object, you "
332412"should ensure that it's a dictionary before attempting to examine its "
333413"contents."
334414msgstr ""
415+ "直接 ``__annotations__`` メンバーにアクセスするなら、まず値が辞書であることを"
416+ "チェックしてから中身を調べるべきです。"
335417
336418#: ../../howto/annotations.rst:200
337419msgid "You should avoid modifying ``__annotations__`` dicts."
338- msgstr ""
420+ msgstr "``__annotations__`` 辞書を書き換えるのは避けるべきです。 "
339421
340422#: ../../howto/annotations.rst:202
341423msgid ""
342424"You should avoid deleting the ``__annotations__`` attribute of an object."
343- msgstr ""
425+ msgstr "``__annotations__`` 属性を削除するのは避けるべきです。 "
344426
345427#: ../../howto/annotations.rst:207
346428msgid "``__annotations__`` Quirks"
347- msgstr ""
429+ msgstr "``__annotations__`` の注意点 "
348430
349431#: ../../howto/annotations.rst:209
350432msgid ""
0 commit comments