Skip to content

Commit ee23dd3

Browse files
committed
cap24: revisão de estilo WIP
1 parent 8cccafc commit ee23dd3

4 files changed

Lines changed: 242 additions & 124 deletions

File tree

code/24-class-metaprog/checked/initsub/checkedlib.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
2323
# tag::MOVIE_TYPE_VALIDATION[]
2424
25-
>>> blockbuster = Movie(title='Avatar', year=2009, box_office='billions')
25+
>>> megahit = Movie(title='Avatar', year=2009,
26+
... box_office='billions')
2627
Traceback (most recent call last):
2728
...
2829
TypeError: 'billions' is not compatible with box_office:float
@@ -45,7 +46,7 @@
4546
4647
Providing extra arguments to the constructor is not allowed::
4748
48-
>>> blockbuster = Movie(title='Avatar', year=2009, box_office=2000,
49+
>>> megahit = Movie(title='Avatar', year=2009, box_office=2000,
4950
... director='James Cameron')
5051
Traceback (most recent call last):
5152
...
@@ -86,7 +87,8 @@ def __set__(self, instance: Any, value: Any) -> None:
8687
value = self.constructor(value) # <5>
8788
except (TypeError, ValueError) as e: # <6>
8889
type_name = self.constructor.__name__
89-
msg = f'{value!r} is not compatible with {self.name}:{type_name}'
90+
msg = (f'{value!r} is not compatible with ' +
91+
f'{self.name}:{type_name}')
9092
raise TypeError(msg) from e
9193
instance.__dict__[self.name] = value # <7>
9294
# end::CHECKED_FIELD[]
@@ -124,7 +126,8 @@ def __flag_unknown_attrs(self, *names: str) -> NoReturn: # <5>
124126
plural = 's' if len(names) > 1 else ''
125127
extra = ', '.join(f'{name!r}' for name in names)
126128
cls_name = repr(self.__class__.__name__)
127-
raise AttributeError(f'{cls_name} object has no attribute{plural} {extra}')
129+
raise AttributeError(f'{cls_name} object has ' +
130+
f'no attribute{plural} {extra}')
128131

129132
def _asdict(self) -> dict[str, Any]: # <6>
130133
return {

code/24-class-metaprog/factories.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,11 @@
2929

3030

3131
# tag::RECORD_FACTORY[]
32-
from typing import Union, Any
3332
from collections.abc import Iterable, Iterator
3433

35-
FieldNames = Union[str, Iterable[str]] # <1>
34+
FieldNames = str | Iterable[str] # <1>
3635

37-
def record_factory(cls_name: str, field_names: FieldNames) -> type[tuple]: # <2>
36+
def record_factory(cls_name: str, field_names: FieldNames) -> type: # <2>
3837

3938
slots = parse_identifiers(field_names) # <3>
4039

@@ -44,7 +43,7 @@ def __init__(self, *args, **kwargs) -> None: # <4>
4443
for name, value in attrs.items():
4544
setattr(self, name, value)
4645

47-
def __iter__(self) -> Iterator[Any]: # <5>
46+
def __iter__(self) -> Iterator: # <5>
4847
for name in self.__slots__:
4948
yield getattr(self, name)
5049

online/cap23.adoc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,7 @@ implementado como uma classe, com `+__call__+`, também precisa implementar
698698
`+__get__+` se quiser funcionar com métodos.
699699

700700
A https://fpy.li/pep487[_PEP 487—Simpler customization of class creation_]
701-
(Customização mais simples da criação de classes)
701+
(Customização simplificada da criação de classes)
702702
introduziu o método especial `+__set_name__+` e inclui um exemplo de um
703703
https://fpy.li/23-7[«descritor de validação»].
704704

0 commit comments

Comments
 (0)