Skip to content

Type annotation error: str = None should be Optional[str] #4831

@leedien8

Description

@leedien8

Prerequisites

  • Write a descriptive title.
  • Make sure you are able to repro it on the latest released version
  • Search the existing issues, especially the pinned issues.

Exception report

Last 200 Keys:
 k h - d i e n l l / D e s k t o p / s o l a r - w a r e h o u s e - o p t i m i z a t i o n 1 
. 1 Space ( 1 ) . p y " Enter
 & Space C : / U s e r s / b k h - d i e n l l / A p p D a t a / L o c a l / P r o g r a m s / 
P y t h o n / P y t h o n 3 1 3 / p y t h o n . e x e Space " c : / U s e r s / b k h - d i e n l l / D e s k t o p / s o l a r - w a r e h o u s e - o p t i m i z a t i o n 1 . 1 Space ( 1 
) . p y " Enter


Exception:
System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was -5.
   at System.Console.SetCursorPosition(Int32 left, Int32 top)
   at Microsoft.PowerShell.PSConsoleReadLine.ReallyRender(RenderData renderData, String defaultColor)
   at Microsoft.PowerShell.PSConsoleReadLine.ForceRender()
   at Microsoft.PowerShell.PSConsoleReadLine.Insert(Char c)
   at Microsoft.PowerShell.PSConsoleReadLine.AcceptLineImpl(Boolean validate)
   at Microsoft.PowerShell.PSConsoleReadLine.ProcessOneKey(ConsoleKeyInfo key, Dictionary`2 dispatchTable, Boolean ignoreIfNoAction, Object arg)
   at Microsoft.PowerShell.PSConsoleReadLine.InputLoop()
   at Microsoft.PowerShell.PSConsoleReadLine.ReadLine(Runspace runspace, EngineIntrinsics engineIntrinsics)

Screenshot

Image

When using type checkers (e.g., mypy, pylance), the following function signature causes a type error:

def _load_excel_data_with_fallback(self, filename: str, sheet_name: str = None):

Error: "Expression of type 'None' cannot be assigned to parameter of type 'str'"

Expected:
No type error if the argument can be None.

Fix:
Change the annotation to:
def _load_excel_data_with_fallback(self, filename: str, sheet_name: Optional[str] = None):

Environment:

  • Python 3.11.3
  • Windows 10
  • Linter: mypy/pylance

Reference: mypy common issues

Environment data

**Environment:**
- Windows 10
- PowerShell version: [run `$PSVersionTable.PSVersion`]
- PSReadLine version: [run `(Get-Module PSReadline).Version`]
- BufferWidth: [run `[console]::BufferWidth`]
- BufferHeight: [run `[console]::BufferHeight`]

Steps to reproduce

Steps to reproduce:

  1. Open PowerShell.
  2. Run a Python script that produces a lot of output (e.g., print statements in a loop).

Expected behavior

Expected:
No type error if the argument can be None.

Actual behavior

System.ArgumentOutOfRangeException: The value must be greater than or equal to zero and less than the console's buffer size in that dimension.
Parameter name: top
Actual value was -5.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions