Error of isinstance() types

Asked

Viewed 48 times

-4

I have a function that receives a date of birth and another reference date to calculate the age of an individual in reference date. It should return me a list containing how many days, months and years respectively the (a) individual will have.

def calculaIdade(dtNascimento, dtLimite):
    """
    Solução do @[Tomasz Zielinski] e @Williams
    :param dtNascimento:
    :param dtLimite:
    :return: List[dias, meses, anos]
    """
    
    print(f'dtNascimento ({type(dtNascimento)}): {dtNascimento}')
    print(f'dtNascimento ({type(dtLimite)}): {dtLimite}')

    idadeRelativa = relativedelta(dtNascimento, dtLimite)

    return [idadeRelativa.days, idadeRelativa.months, idadeRelativa.years]

To that end, I’m using the library dateutil Python, as you can see in my code above. The problem is that the relativedelta method is returning me a very strange error:

Traceback (most recent call last):
  File "/home/israeldev/Projetos/entrevistaController.py", line 273, in trocaTelaCentral
    calculaAposentadoria = CalculosAposentadoria(self.processoModelo, self.clienteAtual, db=self.db)
  File "/home/israeldev/Projetos/aposentadoria.py", line 56, in __init__
    RegraTransicao.reducaoIdadeMinima: self.regraRedIdadeMinima(),
  File "/home/israeldev/Projetos/aposentadoria.py", line 387, in regraRedIdadeMinima
    idadeCliente = calculaIdade(dataNascimento, self.dibAtual)
  File "/home/israeldev/Projetos/util/dateHelper.py", line 17, in calculaIdade
    idadeRelativa = relativedelta(dtNascimento, dtLimite)
  File "/home/israeldev/Projetos/lib/python3.9/site-packages/dateutil/relativedelta.py", line 114, in __init__
    if not (isinstance(dt1, datetime.date) and
TypeError: isinstance() arg 2 must be a type or tuple of types
dtNascimento (<class 'datetime.datetime'>): 1968-03-28 00:00:00
dtNascimento (<class 'datetime.datetime'>): 2020-06-15 00:00:00

I’m using:

  • Python 3.9.5
  • Linux (Popos)

Just as a complement, I looked here in the related questions and found some things, but I did not succeed in my solution, probably because I did not understand very well the explanations I found... Below one of them...

https://stackoverflow.com/questions/14681096/typeerror-isinstance-arg-2-must-be-a-type-or-tuple-of-types/44839264

  • The code you entered does not generate the given error: https://ideone.com/rn3kdr - maybe the problem lies elsewhere in the code that was not shown

  • Check whether the parameters dtNascimento and dtLimite are of the type datetime.datetime.

1 answer

0

I am not sure what kind of data will be passed on in this role, but follows an example of how relativedelta:

exemplo de uso de timedelta

  • Please provide additional details in your reply. As it is currently written, it is difficult to understand your solution.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.