INPC (e erro)
rodolfo-viana opened this issue · 1 comments
rodolfo-viana commented
Um índice que é importante mas não está no escopo original do projeto é o INPC.
Agora pela manhã fiz um adapter
para ele, pois usa muito do código-base do IPCA e me pareceu simples:
from datetime import date
from decimal import Decimal
from typing import NamedTuple
from calculadora_do_cidadao.base import Adapter
from calculadora_do_cidadao.months import MONTHS
from calculadora_do_cidadao.typing import MaybeIndexesGenerator
class Inpc(Adapter):
file_type = "xls"
url = "ftp://ftp.ibge.gov.br/Precos_Indices_de_Precos_ao_Consumidor/INPC/Serie_Historica/inpc_SerieHist.zip"
IMPORT_KWARGS = {"end_column": 2}
SHOULD_UNZIP = True
def serialize(self, row: NamedTuple) -> MaybeIndexesGenerator:
self.last_year = getattr(self, "last_year", None)
year, month, value = row
if month not in MONTHS.keys():
return
year = int(year or self.last_year)
month = MONTHS[month]
reference_date = self.round_date(date(year, month, 1))
value = Decimal(value) / 100
self.last_year = year
yield reference_date, value
Rodei e funcionou. Ótimo.
Porém, quando rodo tox
, dá erro de tipo:
Input:
from datetime import date
from decimal import Decimal
import pytest
from calculadora_do_cidadao.adapters import Inpc
from calculadora_do_cidadao.base import AdapterDateNotAvailableError
@pytest.mark.parametrize(
"original,value,target,expected",
(
(date(2014, 3, 6), None, None, "1.361007124894175467688242800"),
(date(2011, 5, 8), 9, None, "14.373499236614377437778943450"),
(date(2009, 1, 12), 5, date(2013, 8, 1), "6.410734265150376567640231785"),
),
)
def test_data(original, value, target, expected, inpc_fixture, mocker):
download = mocker.patch("calculadora_do_cidadao.base.Download")
download.return_value.return_value.__enter__.return_value = inpc_fixture
inpc = Inpc()
assert len(inpc.data) == 312
assert inpc.adjust(original, value, target) == Decimal(pytest.approx(expected))
msg = r"This adapter has data from 01/1994 to 12/2019\. 02/2020 is out of range\."
with pytest.raises(AdapterDateNotAvailableError, match=msg):
inpc.adjust(date(2020, 2, 1))
Output:
[...]
__________ test_data[original0-None-None-1.361007124894175467688242800] ___________
original = datetime.date(2014, 3, 6), value = None, target = None
expected = '1.361007124894175467688242800'
inpc_fixture = PosixPath('/home/rodolfo/Documents/Github/calculadora-do-cidadao/tests/fixtures/inpc.xls')
mocker = <pytest_mock.plugin.MockFixture object at 0x7f48bbbd90b8>
@pytest.mark.parametrize(
"original,value,target,expected",
(
(date(2014, 3, 6), None, None, "1.361007124894175467688242800"),
(date(2011, 5, 8), 9, None, "14.373499236614377437778943450"),
(date(2009, 1, 12), 5, date(2013, 8, 1), "6.410734265150376567640231785"),
),
)
def test_data(original, value, target, expected, inpc_fixture, mocker):
download = mocker.patch("calculadora_do_cidadao.base.Download")
download.return_value.return_value.__enter__.return_value = inpc_fixture
inpc = Inpc()
assert len(inpc.data) == 312
> assert inpc.adjust(original, value, target) == Decimal(pytest.approx(expected))
E TypeError: cannot make approximate comparisons to non-numeric values: '1.361007124894175467688242800'
Este erro de tipo se repete nas três respostas esperadas -- não porque estejam erradas, mas porque não são consideradas numéricas (?).
Sabe o motivo?
rodolfo-viana commented
Ops. Achei o erro e já corrigi. Funcionou.
Fechando a issue mais inútil da história. :)