[Python] "Expected type 'SupportsWrite[str]', got 'TextIO' instead"

Discussão em 'Python' iniciado por Stack, Maio 3, 2025.

  1. Stack

    Stack Membro Participativo

    import json

    class Account:
    """Creates a new account"""
    def __init__(self, name: str, account_no: int, pin: int, balance: float):
    self.name = name.capitalize()
    self.account_no = account_no
    self.pin = pin
    self.balance = balance
    self._update_ledger()

    def _update_ledger(self):
    # Gets the current ledgers
    with open("ledgers.json", "r") as fp:
    customers = json.load(fp)
    # Updates the ledgers
    with open("ledgers.json", "w") as fp:
    customers[self.name] = self.__dict__
    json.dump(customers, fp, indent=2)


    My code runs perfectly fine, but my code inspector in pycharm warns me for a potential error saying "Expected type 'SupportsWrite[str]', got 'TextIO' instead" on the variable fp on the last line.

    I just that yellow underline for potential error under fp to disappear.

    Continue reading...

Compartilhe esta Página