[Python] why is pylance not recognizing my @property?

Discussão em 'Python' iniciado por Stack, Março 14, 2025.

  1. Stack

    Stack Membro Participativo

    I have a simple class ConfigFile declared using the @property annotation to define properties. The properties are defined as follows:

    @property
    def sections(self) -> 'list[ConfFileSection]':
    """This is a list of ConfFileSection objects, containing the names of all the sections of the config."""
    if not self.__sectslist_:
    self.__sectslist_ = []
    return self.__sectslist_

    @sections.setter
    def sections(self, action, *args)->None:

    if not self.__sectslist_:
    self.__sectslist_ = []
    if action == 'update':
    for a in args:
    if type(a) == ConfFileSection:
    self.__sectslist_.append(a)
    else:
    raise CfpTypeError()
    elif action == 'overwrite':
    self.__sectslist_ = []
    for a in args:
    self.__sectslist_.append(a)
    elif action == 'empty':
    self.__sectslist_ = []
    elif action == 'refresh':
    self.__secnames = self.__get_section_names_from_conffile()
    for name in self.__secnames:
    pass

    @property
    def location_dirpath(self) -> str:
    return self.__locdirpath

    @location_dirpath.setter
    def location_dirpath(self, lp) -> None:
    self.__locdirpath = lp

    @property
    def filename(self) -> str:
    return self.__file_name

    @filename.setter
    def filename(self, fname) -> None:
    self.__file_name = fname


    As you can see, it defines three properties. The init method has the following signature:

    def __init__(self, sections: list=[], location_dirpath: str, filename: str):


    However, when I try defining the init method, Pylance is underlining the location_dirpath: str portion in red. The reason it gives is:

    non default argument follows default argument


    The location_dirpath property is defined using the same annotation as the rest, so why is Pylance not accepting it as an argument?

    Continue reading...

Compartilhe esta Página