Skip to content

Commit

Permalink
chore: Upgrade attrs syntax
Browse files Browse the repository at this point in the history
`field` and `define`was added in v20.1.0 in 2020 as Python 3.6+ replacements of older syntax.

`field` replaces `attr.ib`, and `define` replaces
`attr.s`.
  • Loading branch information
sondrelg committed May 2, 2022
1 parent 2699c5a commit 519dc66
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 125 deletions.
102 changes: 51 additions & 51 deletions netsgiro/objects.py
Expand Up @@ -7,7 +7,7 @@
from typing import OrderedDict as OrderedDictType
from typing import TypeVar, Union, cast

import attr
from attrs import Factory, define, field
from attrs.validators import instance_of, optional

from netsgiro.converters import (
Expand Down Expand Up @@ -53,7 +53,7 @@
R = TypeVar('R', bound='Record')


@attr.s
@define
class Transmission:
"""Transmission is the top-level object.
Expand All @@ -62,24 +62,24 @@ class Transmission:
"""

#: Data transmitters unique enumeration of the transmission. String of 7 digits.
number: str = attr.ib(validator=str_of_length(7))
number: str = field(validator=str_of_length(7))

#: Data transmitter's Nets ID. String of 8 digits.
data_transmitter: str = attr.ib(validator=str_of_length(8))
data_transmitter: str = field(validator=str_of_length(8))

#: Data recipient's Nets ID. String of 8 digits.
data_recipient: str = attr.ib(validator=str_of_length(8))
data_recipient: str = field(validator=str_of_length(8))

#: For OCR Giro files from Nets, this is Nets' processing date.
#:
#: For AvtaleGiro payment request, the earliest due date in the
#: transmission is automatically used.
date: Optional[datetime.date] = attr.ib(
date: Optional[datetime.date] = field(
default=None, validator=optional(instance_of(datetime.date))
)

#: List of assignments.
assignments: List['Assignment'] = attr.ib(default=attr.Factory(list), repr=False)
assignments: List['Assignment'] = field(default=Factory(list), repr=False)

@classmethod
def from_records(cls, records: List[R]) -> 'Transmission':
Expand Down Expand Up @@ -214,42 +214,42 @@ def get_total_amount(self) -> Decimal:
TR = TypeVar('TR', bound=TransactionRecord)


@attr.s
@define
class Assignment:
"""An Assignment groups multiple transactions within a transmission.
Use :meth:`netsgiro.Transmission.add_assignment` to create assignments.
"""

#: The service code. One of :class:`~netsgiro.ServiceCode`.
service_code: ServiceCode = attr.ib(converter=to_service_code)
service_code: ServiceCode = field(converter=to_service_code)

#: The transaction type. One of :class:`~TransactionType`.
type: AssignmentType = attr.ib(converter=to_assignment_type)
type: AssignmentType = field(converter=to_assignment_type)

#: The assignment number. String of 7 digits.
number: str = attr.ib(validator=str_of_length(7))
number: str = field(validator=str_of_length(7))

#: The payee's bank account. String of 11 digits.
account: str = attr.ib(validator=str_of_length(11))
account: str = field(validator=str_of_length(11))

#: Used for OCR Giro.
#:
#: The payee's agreement ID with Nets. String of 9 digits.
agreement_id: Optional[str] = attr.ib(default=None, validator=optional(str_of_length(9)))
agreement_id: Optional[str] = field(default=None, validator=optional(str_of_length(9)))

#: Used for OCR Giro.
#:
#: The date the assignment was generated by Nets.
date: Optional[datetime.date] = attr.ib(
date: Optional[datetime.date] = field(
default=None, validator=optional(instance_of(datetime.date))
)

#: List of transaction objects, like :class:`~netsgiro.Agreement`,
#: :class:`~netsgiro.PaymentRequest`, :class:`~netsgiro.Transaction`.
transactions: TS = attr.ib(default=attr.Factory(list), repr=False)
transactions: TS = field(default=Factory(list), repr=False)

_next_transaction_number: int = attr.ib(default=1, init=False)
_next_transaction_number: int = field(default=1, init=False)

@classmethod
def from_records(cls, records: List[R]) -> 'Assignment':
Expand Down Expand Up @@ -500,7 +500,7 @@ def get_latest_transaction_date(self) -> Optional[datetime.date]:
return self._get_earliest_or_latest_transaction_date(latest=True)


@attr.s
@define
class Agreement:
"""Agreement contains an AvtaleGiro agreement update.
Expand All @@ -510,24 +510,24 @@ class Agreement:
"""

#: The service code. One of :class:`~netsgiro.ServiceCode`.
service_code: ServiceCode = attr.ib(converter=to_service_code)
service_code: ServiceCode = field(converter=to_service_code)

#: Transaction number. Unique and ordered within an assignment.
number: int = attr.ib(validator=instance_of(int))
number: int = field(validator=instance_of(int))

#: Type of agreement registration update.
#: One of :class:`~AvtaleGiroRegistrationType`.
registration_type: 'AvtaleGiroRegistrationType' = attr.ib(
registration_type: 'AvtaleGiroRegistrationType' = field(
converter=to_avtalegiro_registration_type
)

#: KID number to identify the customer and invoice.
kid: Optional[str] = attr.ib(validator=optional(instance_of(str)))
kid: Optional[str] = field(validator=optional(instance_of(str)))

#: Whether the payer wants notification about payment requests.
notify: bool = attr.ib(validator=instance_of(bool))
notify: bool = field(validator=instance_of(bool))

TRANSACTION_TYPE = TransactionType.AVTALEGIRO_AGREEMENT
TRANSACTION_TYPE: ClassVar[TransactionType] = TransactionType.AVTALEGIRO_AGREEMENT

@classmethod
def from_records(cls, records: List[AvtaleGiroAgreement]) -> 'Agreement':
Expand Down Expand Up @@ -557,7 +557,7 @@ def to_records(self) -> Iterable[AvtaleGiroAgreement]:
)


@attr.s
@define
class PaymentRequest:
"""PaymentRequest contains an AvtaleGiro payment request or cancellation.
Expand All @@ -568,35 +568,35 @@ class PaymentRequest:
"""

#: The service code. One of :class:`~netsgiro.ServiceCode`.
service_code: ServiceCode = attr.ib(converter=to_service_code)
service_code: ServiceCode = field(converter=to_service_code)

#: The transaction type. One of :class:`~TransactionType`.
type: TransactionType = attr.ib(converter=to_transaction_type)
type: TransactionType = field(converter=to_transaction_type)

#: Transaction number. Unique and ordered within an assignment.
number: int = attr.ib(validator=instance_of(int))
number: int = field(validator=instance_of(int))

#: The due date.
date: datetime.date = attr.ib(validator=instance_of(datetime.date))
date: datetime.date = field(validator=instance_of(datetime.date))

#: Transaction amount in NOK with two decimals.
amount: Decimal = attr.ib(converter=Decimal)
amount: Decimal = field(converter=Decimal)

#: KID number to identify the customer and invoice.
kid: Optional[str] = attr.ib(validator=optional(instance_of(str)))
kid: Optional[str] = field(validator=optional(instance_of(str)))

#: This is a specification line that will, if set, be displayed on the
#: payers account statement. Alphanumeric, max 25 chars.
reference: Optional[str] = attr.ib(validator=optional(instance_of(str)))
reference: Optional[str] = field(validator=optional(instance_of(str)))

#: This is up to 42 lines of 80 chars each of free text used by the bank to
#: notify the payer about the payment request. It is not used if the payee
#: is responsible for notifying the payer.
text: Optional[str] = attr.ib(validator=optional(instance_of(str)))
text: Optional[str] = field(validator=optional(instance_of(str)))

#: The value is only used to help the payee cross-reference reports from
#: Nets with their own records. It is not vi.attr.ible to the payer.
payer_name: Optional[str] = attr.ib(validator=optional(instance_of(str)))
payer_name: Optional[str] = field(validator=optional(instance_of(str)))

@property
def amount_in_cents(self) -> int:
Expand Down Expand Up @@ -654,7 +654,7 @@ def to_records(
)


@attr.s
@define
class Transaction:
"""Transaction contains an OCR Giro transaction.
Expand All @@ -664,54 +664,54 @@ class Transaction:
"""

#: The service code. One of :class:`~netsgiro.ServiceCode`.
service_code: ServiceCode = attr.ib(converter=to_service_code)
service_code: ServiceCode = field(converter=to_service_code)

#: The transaction type. One of :class:`~TransactionType`.
type: TransactionType = attr.ib(converter=to_transaction_type)
type: TransactionType = field(converter=to_transaction_type)

#: Transaction number. Unique and ordered within an assignment.
number: int = attr.ib(validator=instance_of(int))
number: int = field(validator=instance_of(int))

#: Nets' processing date.
date: datetime.date = attr.ib(validator=instance_of(datetime.date))
date: datetime.date = field(validator=instance_of(datetime.date))

#: Transaction amount in NOK with two decimals.
amount: Decimal = attr.ib(converter=Decimal)
amount: Decimal = field(converter=Decimal)

#: KID number to identify the customer and invoice.
kid: Optional[str] = attr.ib(validator=optional(instance_of(str)))
kid: Optional[str] = field(validator=optional(instance_of(str)))

#: The value depends on the payment method.
reference: Optional[str] = attr.ib(validator=optional(instance_of(str)))
reference: Optional[str] = field(validator=optional(instance_of(str)))

#: Up to 40 chars of free text from the payment terminal.
text: Optional[str] = attr.ib(validator=optional(instance_of(str)))
text: Optional[str] = field(validator=optional(instance_of(str)))

#: Used for OCR Giro.
centre_id: Optional[str] = attr.ib(validator=optional(str_of_length(2)))
centre_id: Optional[str] = field(validator=optional(str_of_length(2)))

#: Used for OCR Giro.
day_code: Optional[int] = attr.ib(validator=optional(instance_of(int)))
day_code: Optional[int] = field(validator=optional(instance_of(int)))

#: Used for OCR Giro.
partial_settlement_number: Optional[int] = attr.ib(validator=optional(instance_of(int)))
partial_settlement_number: Optional[int] = field(validator=optional(instance_of(int)))

#: Used for OCR Giro.
partial_settlement_serial_number: Optional[str] = attr.ib(validator=optional(str_of_length(5)))
partial_settlement_serial_number: Optional[str] = field(validator=optional(str_of_length(5)))

#: Used for OCR Giro.
sign: Optional[str] = attr.ib(validator=optional(str_of_length(1)))
sign: Optional[str] = field(validator=optional(str_of_length(1)))

#: Used for OCR Giro.
form_number: Optional[str] = attr.ib(validator=optional(str_of_length(10)))
form_number: Optional[str] = field(validator=optional(str_of_length(10)))

#: Used for OCR Giro.
bank_date: Optional[datetime.date] = attr.ib(validator=optional(instance_of(datetime.date)))
bank_date: Optional[datetime.date] = field(validator=optional(instance_of(datetime.date)))

#: Used for OCR Giro.
debit_account: Optional[str] = attr.ib(validator=optional(str_of_length(11)))
debit_account: Optional[str] = field(validator=optional(str_of_length(11)))

_filler: Optional[str] = attr.ib(validator=optional(str_of_length(7)))
_filler: Optional[str] = field(validator=optional(str_of_length(7)))

@property
def amount_in_cents(self) -> int:
Expand Down

0 comments on commit 519dc66

Please sign in to comment.