10000 Support changing a variable's type · Issue #257 · microsoft/pylance-release · GitHub
[go: up one dir, main page]

Skip to content
Support changing a variable's type #257
@neighthan

Description

@neighthan

If I have a variable of known type and want that name to now reference an object of another type, pylance complains. E.g.:

def parse_int(x: str):
    x = int(x)
    return x

gives

Expression of type "int" cannot be assigned to declared type "str"
  "int" is incompatible with "str" Pylance (reportGeneralTypeIssues)

I can see how this would sometimes be useful to catch, but I would personally prefer for this not to be seen as an issue. I did try to explicitly declare the type the second time, to show that I'm intentionally changing it, but that just gives a different error:

def parse_int(x: str):
    x: int = int(x)
    return x
Parameter declaration "x" is obscured by a declaration of the same name Pylance (reportGeneralTypeIssues)

I believe mypy supports an option to allow such type changes; could support for this be added to pylance?

(I'm not sure if this request should be on pylance or pyright; let me know if I'm in the wrong place or if there's already a configuration option I can change somewhere to achieve this)

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      0