1- from typing import Optional
2- from mypy .plugin import Plugin , FunctionContext
3- from mypy .types import Type , Instance
4- from mypy .nodes import ARG_POS
1+ from mypy .plugin import FunctionContext , Plugin
2+ from mypy .types import Instance , Type
3+
54
65class CompileFilenamePlugin (Plugin ):
76 def get_function_hook (self , fullname : str ):
@@ -10,24 +9,24 @@ def get_function_hook(self, fullname: str):
109 return compile_hook
1110 return None
1211
12+
1313def compile_hook (ctx : FunctionContext ) -> Type :
1414 # Arguments to compile: source, filename, mode, ...
1515 # filename is arg index 1 (zero-based)
1616 if len (ctx .arg_types ) > 1 and ctx .arg_types [1 ]:
1717 filename_type = ctx .arg_types [1 ][0 ] # first argument passed for filename param
1818 if is_bytearray_type (filename_type ):
19- ctx .api .fail (
20- "Passing 'bytearray' as filename to 'compile()' is not allowed" ,
21- ctx .args [1 ][0 ]
22- )
19+ ctx .api .fail ("Passing 'bytearray' as filename to 'compile()' is not allowed" , ctx .args [1 ][0 ])
2320 return ctx .default_return_type
2421
22+
2523def is_bytearray_type (typ : Type ) -> bool :
2624 # Check if the type is exactly bytearray
2725 if isinstance (typ , Instance ):
2826 # The full name for builtins.bytearray is 'builtins.bytearray'
2927 return typ .type .fullname == "builtins.bytearray"
3028 return False
3129
30+
3231def plugin (version : str ):
3332 return CompileFilenamePlugin
0 commit comments