@@ -540,6 +540,82 @@ def findConfigFile(
540540 return None
541541
542542
543+ @hookimpl
544+ def pylsp_code_actions (
545+ config : Config ,
546+ workspace : Workspace ,
547+ document : Document ,
548+ range : Dict ,
549+ context : Dict ,
550+ ) -> List [Dict ]:
551+ """
552+ Provide code actions to ignore errors.
553+
554+ Parameters
555+ ----------
556+ config : pylsp.config.config.Config
557+ Current config.
558+ workspace : pylsp.workspace.Workspace
559+ Current workspace.
560+ document : pylsp.workspace.Document
561+ Document to apply code actions on.
562+ range : Dict
563+ Range argument given by pylsp.
564+ context : Dict
565+ CodeActionContext given as dict.
566+
567+ Returns
568+ -------
569+ List of dicts containing the code actions.
570+ """
571+ actions = []
572+ # Code actions based on diagnostics
573+ for diagnostic in context .get ("diagnostics" , []):
574+ if diagnostic ["source" ] != "mypy" :
575+ continue
576+ code = diagnostic ["code" ]
577+ lineNumberEnd = diagnostic ["range" ]["end" ]["line" ]
578+ line = document .lines [lineNumberEnd ]
579+ endOfLine = len (line ) - 1
580+ start = {"line" : lineNumberEnd , "character" : endOfLine }
581+ edit_range = {"start" : start , "end" : start }
582+ edit = {"range" : edit_range , "newText" : f" # type: ignore[{ code } ]" }
583+
584+ action = {
585+ "title" : f"# type: ignore[{ code } ]" ,
586+ "kind" : "quickfix" ,
587+ "diagnostics" : [diagnostic ],
588+ "edit" : {"changes" : {document .uri : [edit ]}},
589+ }
590+ actions .append (action )
591+ if context .get ("diagnostics" , []) != []:
592+ return actions
593+
594+ # Code actions based on current selected range
595+ for diagnostic in last_diagnostics [document .path ]:
596+ lineNumberStart = diagnostic ["range" ]["start" ]["line" ]
597+ lineNumberEnd = diagnostic ["range" ]["end" ]["line" ]
598+ rStart = range ["start" ]["line" ]
599+ rEnd = range ["end" ]["line" ]
600+ if (rStart <= lineNumberStart and rEnd >= lineNumberStart ) or (
601+ rStart <= lineNumberEnd and rEnd >= lineNumberEnd
602+ ):
603+ code = diagnostic ["code" ]
604+ line = document .lines [lineNumberEnd ]
605+ endOfLine = len (line ) - 1
606+ start = {"line" : lineNumberEnd , "character" : endOfLine }
607+ edit_range = {"start" : start , "end" : start }
608+ edit = {"range" : edit_range , "newText" : f" # type: ignore[{ code } ]" }
609+ action = {
610+ "title" : f"# type: ignore[{ code } ]" ,
611+ "kind" : "quickfix" ,
612+ "edit" : {"changes" : {document .uri : [edit ]}},
613+ }
614+ actions .append (action )
615+
616+ return actions
617+
618+
543619@atexit .register
544620def close () -> None :
545621 """
0 commit comments