]> Savannah Git Hosting - gnulib.git/commitdiff
gnulib-tool.py: Add type hints to classes.
authorCollin Funk <collin.funk1@gmail.com>
Tue, 30 Apr 2024 05:20:31 +0000 (22:20 -0700)
committerCollin Funk <collin.funk1@gmail.com>
Tue, 30 Apr 2024 05:20:31 +0000 (22:20 -0700)
* pygnulib/*.py: Add type hints for all instance and class variables.
* pygnulib/GLMakefileTable.py (GLMakefileTable.__getitem__): Fix return
type hint since the dictionary has str values.

ChangeLog
pygnulib/GLConfig.py
pygnulib/GLEmiter.py
pygnulib/GLError.py
pygnulib/GLFileSystem.py
pygnulib/GLImport.py
pygnulib/GLMakefileTable.py
pygnulib/GLModuleSystem.py
pygnulib/GLTestDir.py

index 0fdeee8cf90bb9fa01c2dbde153ce828a5812607..bd5bb93c0bfb2ca2adfb9407b97bc9d779f2d6c6 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2024-04-29  Collin Funk  <collin.funk1@gmail.com>
+
+       gnulib-tool.py: Add type hints to classes.
+       * pygnulib/*.py: Add type hints for all instance and class variables.
+       * pygnulib/GLMakefileTable.py (GLMakefileTable.__getitem__): Fix return
+       type hint since the dictionary has str values.
+
 2024-04-29  Collin Funk  <collin.funk1@gmail.com>
 
        gnulib-tool.py: Emit libtests in testdirs generated Makefile.am.
index 16fa490fc61e89da3ddb5f3e888b0c8834fda7f8..92aa49d700ddbc6a62609858fa12e9621e994859 100644 (file)
@@ -41,6 +41,8 @@ class GLConfig:
     By default all attributes are set to empty string, empty list or zero.
     The most common value, however, is a None value.'''
 
+    table: dict[str, Any]
+
     def __init__(self,
                  destdir: str | None = None,
                  localpath: list[str] | None = None,
index ed6eae499764ee515253c04782a4946810b8bdbb..3fbf796aaab09c46be2dbeefde24f59049ea7cd6 100644 (file)
@@ -98,6 +98,9 @@ def _eliminate_NMD(snippet: str, automake_subdir: bool) -> str:
 class GLEmiter:
     '''This class is used to emit the contents of necessary files.'''
 
+    info: GLInfo
+    config: GLConfig
+
     def __init__(self, config: GLConfig) -> None:
         '''Create GLEmiter instance.'''
         self.info = GLInfo()
index 184d65f59c52ac5be047d7a099d21a3e6859147c..4288820c976c221a6448421587876ef582a0f09c 100644 (file)
@@ -19,6 +19,7 @@ from __future__ import annotations
 # Define global imports
 #===============================================================================
 import os
+from typing import Any
 
 
 #===============================================================================
@@ -27,7 +28,11 @@ import os
 class GLError(Exception):
     '''Exception handler for pygnulib classes.'''
 
-    def __init__(self, errno: int, errinfo: str | float | None = None) -> None:
+    errno: int
+    errinfo: Any
+    args: tuple[int, Any]
+
+    def __init__(self, errno: int, errinfo: Any = None) -> None:
         '''Each error has following parameters:
         errno: code of error; used to catch error type
           1: file does not exist in GLFileSystem: <file>
index 028ba3885ed8be7ce403530076754362b249c07c..f8b7f54ab38dae511c685a488956be8e88bb55bc 100644 (file)
@@ -46,6 +46,8 @@ class GLFileSystem:
     Its main method lookup(file) is used to find file in these directories or
     combine it using Linux 'patch' utility.'''
 
+    config: GLConfig
+
     def __init__(self, config: GLConfig) -> None:
         '''Create new GLFileSystem instance. The only argument is localpath,
         which can be an empty list.'''
@@ -139,6 +141,13 @@ class GLFileSystem:
 class GLFileAssistant:
     '''GLFileAssistant is used to help with file processing.'''
 
+    original: str | None
+    rewritten: str | None
+    added: list[str]
+    config: GLConfig
+    transformers: dict[str, tuple[re.Pattern, str] | None]
+    filesystem: GLFileSystem
+
     def __init__(self, config: GLConfig, transformers: dict[str, tuple[re.Pattern, str] | None] | None = None) -> None:
         '''Create GLFileAssistant instance.
 
index 47c0e835557c0eee40d65b107c5da99648bb639b..63877342d2c3319030673d2ed262e358bc25596e 100644 (file)
@@ -60,6 +60,14 @@ class GLImport:
     scripts. However, if user needs just to use power of gnulib-tool, this class
     is a very good choice.'''
 
+    mode: int
+    config: GLConfig
+    cache: GLConfig
+    emitter: GLEmiter
+    modulesystem: GLModuleSystem
+    moduletable: GLModuleTable
+    makefiletable: GLMakefileTable
+
     def __init__(self, config: GLConfig, mode: int) -> None:
         '''Create GLImport instance.
         The first variable, mode, must be one of the values of the MODES dict
index 16e22f914f3254dab3e5baff64afc46ab33a7e76..efd276d20cf4ed0438fb11f53b66e6578a6390be 100644 (file)
@@ -33,6 +33,9 @@ class GLMakefileTable:
     An edit may be removed; this is done by removing its 'var' key but
     keeping it in the list. Removed edits must be ignored.'''
 
+    config: GLConfig
+    table: list[dict[str, str | bool]]
+
     def __init__(self, config: GLConfig) -> None:
         '''Create GLMakefileTable instance.'''
         if type(config) is not GLConfig:
@@ -41,7 +44,7 @@ class GLMakefileTable:
         self.config = config
         self.table = []
 
-    def __getitem__(self, y: int) -> dict[str, bool]:
+    def __getitem__(self, y: int) -> dict[str, str | bool]:
         '''x.__getitem__(y) = x[y]'''
         if type(y) is not int:
             raise TypeError('indices must be integers, not %s'
index ba1c57eb3f420889fd140f22ea1e2746c9690732..a5afb5af6f8ead64af75602fb768329c3570ccb7 100644 (file)
@@ -24,6 +24,7 @@ import sys
 import hashlib
 import subprocess as sp
 from collections import defaultdict
+from typing import Any, ClassVar
 from .constants import (
     DIRS,
     ENCS,
@@ -46,6 +47,9 @@ class GLModuleSystem:
     '''GLModuleSystem is used to operate with module system using dynamic
     searching and patching.'''
 
+    config: GLConfig
+    filesystem: GLFileSystem
+
     def __init__(self, config: GLConfig) -> None:
         '''Create new GLModuleSystem instance. Some functions use GLFileSystem class
         to look up a file in localpath or gnulib directories, or combine it through
@@ -155,14 +159,25 @@ class GLModule:
     path. GLModule can get all information about module, get its dependencies,
     files, etc.'''
 
-    section_label_pattern = \
+    # Regular expression matching the start of a section in the module description.
+    section_label_pattern: ClassVar[re.Pattern] = \
         re.compile(r'^(Description|Comment|Status|Notice|Applicability|'
                    + r'Files|Depends-on|configure\.ac-early|configure\.ac|'
                    + r'Makefile\.am|Include|Link|License|Maintainer):$',
                    re.M)
 
     # List of characters allowed in shell identifiers.
-    shell_id_chars = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
+    shell_id_chars: ClassVar[str] = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_'
+
+    cache: dict[str, Any]
+    content: str
+    name: str
+    path: str
+    patched: bool
+    config: GLConfig
+    filesystem: GLFileSystem
+    modulesystem: GLModuleSystem
+    sections: dict[str, str]
 
     def __init__(self, config: GLConfig, name: str, path: str, patched: bool = False) -> None:
         '''Create new GLModule instance. Arguments are:
@@ -683,6 +698,20 @@ class GLModule:
 class GLModuleTable:
     '''GLModuleTable is used to work with the list of the modules.'''
 
+    dependers: defaultdict[GLModule, set[GLModule]]
+    conditionals: dict[tuple[GLModule, GLModule], str | bool]
+    unconditionals: set[GLModule]
+    base_modules: list[GLModule]
+    main_modules: list[GLModule]
+    tests_modules: list[GLModule]
+    final_modules: list[GLModule]
+    config: GLConfig
+    filesystem: GLFileSystem
+    modulesystem: GLModuleSystem
+    inc_all_direct_tests: bool
+    inc_all_indirect_tests: bool
+    avoids: set[GLModule]
+
     def __init__(self, config: GLConfig, inc_all_direct_tests: bool, inc_all_indirect_tests: bool) -> None:
         '''Create new GLModuleTable instance. If modules are specified, then add
         every module from iterable as unconditional module. If avoids is specified,
index 11b067e085162bc765c721daf222b73dfd96dd41..688224cdcee419b06de3c0845cd733b92945e8e9 100644 (file)
@@ -91,6 +91,14 @@ class GLTestDir:
     '''GLTestDir class is used to create a scratch package with the given
     list of the modules.'''
 
+    config: GLConfig
+    testdir: str
+    emitter: GLEmiter
+    filesystem: GLFileSystem
+    modulesystem: GLModuleSystem
+    assistant: GLFileAssistant
+    makefiletable: GLMakefileTable
+
     def __init__(self, config: GLConfig, testdir: str) -> None:
         '''Create new GLTestDir instance.'''
         if type(config) is not GLConfig:
@@ -845,6 +853,10 @@ class GLMegaTestDir:
     '''GLMegaTestDir class is used to create a mega scratch package with the
     given modules one by one and all together.'''
 
+    config: GLConfig
+    megatestdir: str
+    modulesystem: GLModuleSystem
+
     def __init__(self, config: GLConfig, megatestdir: str) -> None:
         '''Create new GLTestDir instance.'''
         if type(config) is not GLConfig: