66
77from __future__ import annotations
88
9- from typing import Any
10- from typing import Callable
11- from typing import Optional
12- from typing import Tuple
13- from typing import Type
14- from typing import Union
9+ import typing
1510
1611import argparse
1712import importlib
3227
3328_identifier_re = r"^[A-Za-z_]\w*$"
3429
35- ObjectT = Union [ModuleType , Type [Any ]]
30+ ObjectT = typing . Union [ModuleType , typing . Type [typing . Any ]]
3631
3732RESERVED_KEYWORDS = {"async" }
3833ALLOWED_FUNCTIONS = {
@@ -55,9 +50,9 @@ def fix_argument_name(name: str) -> str:
5550def _object_get_props (
5651 repo : GIRepository .Repository ,
5752 obj : GI .ObjectInfo ,
58- ) -> Tuple [list [GIRepository .BaseInfo ], list [GIRepository .BaseInfo ]]:
53+ ) -> typing . Tuple [list [GIRepository .BaseInfo ], list [GIRepository .BaseInfo ]]:
5954 parents : list [GI .ObjectInfo ] = []
60- parent : Optional [GI .ObjectInfo ] = obj .get_parent ()
55+ parent : typing . Optional [GI .ObjectInfo ] = obj .get_parent ()
6156 while parent :
6257 parents .append (parent )
6358 parent = parent .get_parent ()
@@ -114,7 +109,7 @@ def _callable_get_arguments(
114109 current_namespace : str ,
115110 needed_namespaces : set [str ],
116111 can_default : bool = False ,
117- ) -> Tuple [list [str ], list [str ], list [str ]]:
112+ ) -> typing . Tuple [list [str ], list [str ], list [str ]]:
118113 function_args = type .get_arguments ()
119114 accept_optional_args = False
120115 optional_args_name = ""
@@ -216,10 +211,10 @@ class TypeInfo:
216211
217212 def __init__ (
218213 self ,
219- obj : Any ,
220- get_tag : Callable [[TypeInfo ], int ],
221- get_param_type : Callable [[TypeInfo , int ], TypeInfo ],
222- get_interface : Callable [[TypeInfo ], TypeInfo ],
214+ obj : typing . Any ,
215+ get_tag : typing . Callable [[TypeInfo ], int ],
216+ get_param_type : typing . Callable [[TypeInfo , int ], TypeInfo ],
217+ get_interface : typing . Callable [[TypeInfo ], TypeInfo ],
223218 ):
224219 self .obj = obj
225220 self ._get_tag = get_tag
@@ -388,6 +383,7 @@ def _build(
388383 ]
389384
390385 if namespace == "Gtk" :
386+ imports .append ("import os" )
391387 typevars .append (
392388 """CellRendererT = typing.TypeVar(
393389 "CellRendererT",
@@ -398,13 +394,15 @@ def _build(
398394 CellRendererSpinner,
399395 CellRendererText,
400396 CellRendererToggle,
401- )"""
397+ )
398+ WidgetT = typing.TypeVar("WidgetT", bound=Widget)
399+ """
402400 )
403401 elif namespace == "GObject" :
404402 imports .append ("import enum" )
405403
406404 if "cairo" in ns :
407- imports = [ "import cairo" ]
405+ imports . append ( "import cairo" )
408406 typevars .append (
409407 '_SomeSurface = typing.TypeVar("_SomeSurface", bound=cairo.Surface)'
410408 )
@@ -435,10 +433,10 @@ def _build_function_info(
435433 current_namespace : str ,
436434 name : str ,
437435 function : GI .FunctionInfo | GI .VFuncInfo ,
438- in_class : Optional [Any ],
436+ in_class : typing . Optional [typing . Any ],
439437 needed_namespaces : set [str ],
440- return_signature : Optional [str ] = None ,
441- comment : Optional [str ] = None ,
438+ return_signature : typing . Optional [str ] = None ,
439+ comment : typing . Optional [str ] = None ,
442440) -> str :
443441 constructor : bool = False
444442 method : bool = isinstance (function , GI .VFuncInfo )
@@ -495,8 +493,8 @@ def _build_function_info(
495493def _wrapped_strip_boolean_result (
496494 current_namespace : str ,
497495 name : str ,
498- function : Any ,
499- in_class : Optional [Any ],
496+ function : typing . Any ,
497+ in_class : typing . Optional [typing . Any ],
500498 needed_namespaces : set [str ],
501499) -> str :
502500 real_function = function .__wrapped__
@@ -537,8 +535,8 @@ def _wrapped_strip_boolean_result(
537535def _build_function (
538536 current_namespace : str ,
539537 name : str ,
540- function : Any ,
541- in_class : Optional [Any ],
538+ function : typing . Any ,
539+ in_class : typing . Optional [typing . Any ],
542540 needed_namespaces : set [str ],
543541) -> str :
544542 if name .startswith ("_" ) and name not in ALLOWED_FUNCTIONS :
@@ -603,7 +601,9 @@ def _build_function(
603601 return definition
604602
605603
606- def _check_override (prefix : str , name : str , overrides : dict [str , str ]) -> Optional [str ]:
604+ def _check_override (
605+ prefix : str , name : str , overrides : dict [str , str ]
606+ ) -> typing .Optional [str ]:
607607 full_name = _generate_full_name (prefix , name )
608608 if full_name in overrides :
609609 return "# override\n " + overrides [full_name ]
@@ -617,7 +617,7 @@ def _gi_build_stub(
617617 children : list [str ],
618618 needed_namespaces : set [str ],
619619 overrides : dict [str , str ],
620- in_class : Optional [Any ],
620+ in_class : typing . Optional [typing . Any ],
621621 prefix_name : str ,
622622) -> str :
623623 return _gi_build_stub_parts (
@@ -639,18 +639,18 @@ def _gi_build_stub_parts(
639639 children : list [str ],
640640 needed_namespaces : set [str ],
641641 overrides : dict [str , str ],
642- in_class : Optional [Any ],
642+ in_class : typing . Optional [typing . Any ],
643643 prefix_name : str ,
644644) -> tuple [str , list [GI .FieldInfo ]]:
645645 """
646646 Inspect the passed module recursively and build stubs for functions,
647647 classes, etc.
648648 """
649- classes : dict [str , Type [Any ]] = {}
650- functions : dict [str , Callable [..., Any ]] = {}
651- constants : dict [str , Any ] = {}
652- flags : dict [str , Type [Any ]] = {}
653- enums : dict [str , Type [Any ]] = {}
649+ classes : dict [str , typing . Type [typing . Any ]] = {}
650+ functions : dict [str , typing . Callable [..., typing . Any ]] = {}
651+ constants : dict [str , typing . Any ] = {}
652+ flags : dict [str , typing . Type [typing . Any ]] = {}
653+ enums : dict [str , typing . Type [typing . Any ]] = {}
654654
655655 ret = ""
656656
@@ -1049,7 +1049,7 @@ def _gi_build_stub_parts(
10491049 return ret , fields
10501050
10511051
1052- def _find_methods_and_fields (obj : Type [Any ]) -> list [str ]:
1052+ def _find_methods_and_fields (obj : typing . Type [typing . Any ]) -> list [str ]:
10531053 mro = inspect .getmro (obj )
10541054 main_name = _get_gname (mro [0 ])
10551055
@@ -1075,7 +1075,7 @@ def _find_methods_and_fields(obj: Type[Any]) -> list[str]:
10751075 return sorted (list (obj_attrs ))
10761076
10771077
1078- def _get_gname (obj : Type [Any ]) -> Optional [str ]:
1078+ def _get_gname (obj : typing . Type [typing . Any ]) -> typing . Optional [str ]:
10791079 if not hasattr (obj , "__gtype__" ):
10801080 return obj .__name__
10811081 return obj .__gtype__ .name # type: ignore
0 commit comments