1+ from __future__ import annotations
2+
13try :
24 import ctypes
35except ImportError :
4- ctypes = None
6+ ctypes = None # type: ignore[assignment]
57import os
68import pathlib
79import platform
810import re
911import sys
1012import types
13+ import typing
14+
15+ if typing .TYPE_CHECKING :
16+ from collections .abc import Generator
1117
1218import pretend
1319import pytest
1824 _glibc_version_string ,
1925 _glibc_version_string_confstr ,
2026 _glibc_version_string_ctypes ,
27+ _GLibCVersion ,
2128 _is_compatible ,
2229 _parse_elf ,
2330 _parse_glibc_version ,
2431)
2532
2633
2734@pytest .fixture (autouse = True )
28- def clear_lru_cache ():
35+ def clear_lru_cache () -> Generator [ None , None , None ] :
2936 yield
3037 _get_glibc_version .cache_clear ()
3138
3239
3340@pytest .fixture
34- def manylinux_module (monkeypatch : pytest .MonkeyPatch ):
41+ def manylinux_module (monkeypatch : pytest .MonkeyPatch ) -> types . ModuleType :
3542 monkeypatch .setattr (_manylinux , "_get_glibc_version" , lambda * args : (2 , 20 ))
3643 module_name = "_manylinux"
3744 module = types .ModuleType (module_name )
@@ -44,44 +51,54 @@ def manylinux_module(monkeypatch: pytest.MonkeyPatch):
4451 ("attribute" , "glibc" ), [("1" , (2 , 5 )), ("2010" , (2 , 12 )), ("2014" , (2 , 17 ))]
4552)
4653def test_module_declaration (
47- monkeypatch : pytest .MonkeyPatch , manylinux_module , attribute , glibc , tf
54+ monkeypatch : pytest .MonkeyPatch ,
55+ manylinux_module : types .ModuleType ,
56+ attribute : str ,
57+ glibc : tuple [int , int ],
58+ tf : bool ,
4859) -> None :
4960 manylinux = f"manylinux{ attribute } _compatible"
5061 monkeypatch .setattr (manylinux_module , manylinux , tf , raising = False )
51- res = _is_compatible ("x86_64" , glibc )
62+ glibc_version = _GLibCVersion (glibc [0 ], glibc [1 ])
63+ res = _is_compatible ("x86_64" , glibc_version )
5264 assert tf is res
5365
5466
5567@pytest .mark .parametrize (
5668 ("attribute" , "glibc" ), [("1" , (2 , 5 )), ("2010" , (2 , 12 )), ("2014" , (2 , 17 ))]
5769)
5870def test_module_declaration_missing_attribute (
59- monkeypatch : pytest .MonkeyPatch , manylinux_module , attribute , glibc
60- ):
71+ monkeypatch : pytest .MonkeyPatch ,
72+ manylinux_module : types .ModuleType ,
73+ attribute : str ,
74+ glibc : tuple [int , int ],
75+ ) -> None :
6176 manylinux = f"manylinux{ attribute } _compatible"
6277 monkeypatch .delattr (manylinux_module , manylinux , raising = False )
63- assert _is_compatible ("x86_64" , glibc )
78+ glibc_version = _GLibCVersion (glibc [0 ], glibc [1 ])
79+ assert _is_compatible ("x86_64" , glibc_version )
6480
6581
6682@pytest .mark .parametrize (
6783 ("version" , "compatible" ), [((2 , 0 ), True ), ((2 , 5 ), True ), ((2 , 10 ), False )]
6884)
6985def test_is_manylinux_compatible_glibc_support (
70- version , compatible , monkeypatch : pytest .MonkeyPatch
86+ version : tuple [ int , int ], compatible : bool , monkeypatch : pytest .MonkeyPatch
7187) -> None :
7288 monkeypatch .setitem (sys .modules , "_manylinux" , None )
7389 monkeypatch .setattr (_manylinux , "_get_glibc_version" , lambda : (2 , 5 ))
74- assert bool (_is_compatible ("any" , version )) == compatible
90+ glibc_version = _GLibCVersion (version [0 ], version [1 ])
91+ assert bool (_is_compatible ("any" , glibc_version )) == compatible
7592
7693
7794@pytest .mark .parametrize ("version_str" , ["glibc-2.4.5" , "2" ])
78- def test_check_glibc_version_warning (version_str ) -> None :
95+ def test_check_glibc_version_warning (version_str : str ) -> None :
7996 msg = f"Expected glibc version with 2 components major.minor, got: { version_str } "
8097 with pytest .warns (RuntimeWarning , match = re .escape (msg )):
8198 _parse_glibc_version (version_str )
8299
83100
84- @pytest .mark .skipif (not ctypes , reason = "requires ctypes" )
101+ @pytest .mark .skipif (not ctypes , reason = "requires ctypes" ) # type: ignore[truthy-bool]
85102@pytest .mark .parametrize (
86103 ("version_str" , "expected" ),
87104 [
@@ -91,17 +108,17 @@ def test_check_glibc_version_warning(version_str) -> None:
91108 ],
92109)
93110def test_glibc_version_string (
94- version_str , expected , monkeypatch : pytest .MonkeyPatch
111+ version_str : str | bytes , expected : str , monkeypatch : pytest .MonkeyPatch
95112) -> None :
96113 class LibcVersion :
97- def __init__ (self , version_str ) :
114+ def __init__ (self , version_str : str | bytes ) -> None :
98115 self .version_str = version_str
99116
100- def __call__ (self ):
101- return version_str
117+ def __call__ (self ) -> str | bytes :
118+ return self . version_str
102119
103120 class ProcessNamespace :
104- def __init__ (self , libc_version ) :
121+ def __init__ (self , libc_version : LibcVersion ) -> None :
105122 self .gnu_get_libc_version = libc_version
106123
107124 process_namespace = ProcessNamespace (LibcVersion (version_str ))
@@ -131,7 +148,7 @@ def test_glibc_version_string_fail(monkeypatch: pytest.MonkeyPatch) -> None:
131148 [pretend .raiser (ValueError ), pretend .raiser (OSError ), lambda _ : "XXX" ],
132149)
133150def test_glibc_version_string_confstr_fail (
134- monkeypatch : pytest .MonkeyPatch , failure
151+ monkeypatch : pytest .MonkeyPatch , failure : typing . Callable [[ int ], str | None ]
135152) -> None :
136153 monkeypatch .setattr (os , "confstr" , failure , raising = False )
137154 assert _glibc_version_string_confstr () is None
@@ -151,7 +168,7 @@ def test_glibc_version_string_ctypes_missing(monkeypatch: pytest.MonkeyPatch) ->
151168def test_glibc_version_string_ctypes_raise_oserror (
152169 monkeypatch : pytest .MonkeyPatch ,
153170) -> None :
154- def patched_cdll (_name ) :
171+ def patched_cdll (_name : str ) -> None :
155172 raise OSError ("Dynamic loading not supported" )
156173
157174 monkeypatch .setattr (ctypes , "CDLL" , patched_cdll )
@@ -162,27 +179,29 @@ def patched_cdll(_name):
162179def test_is_manylinux_compatible_old () -> None :
163180 # Assuming no one is running this test with a version of glibc released in
164181 # 1997.
165- assert _is_compatible ("any" , (2 , 0 ))
182+ assert _is_compatible ("any" , _GLibCVersion (2 , 0 ))
166183
167184
168185def test_is_manylinux_compatible (monkeypatch : pytest .MonkeyPatch ) -> None :
169186 monkeypatch .setattr (_manylinux , "_glibc_version_string" , lambda : "2.4" )
170- assert _is_compatible ("any" , (2 , 4 ))
187+ assert _is_compatible ("any" , _GLibCVersion (2 , 4 ))
171188
172189
173190def test_glibc_version_string_none (monkeypatch : pytest .MonkeyPatch ) -> None :
174191 monkeypatch .setattr (_manylinux , "_glibc_version_string" , lambda : None )
175- assert not _is_compatible ("any" , (2 , 4 ))
192+ assert not _is_compatible ("any" , _GLibCVersion (2 , 4 ))
176193
177194
178195@pytest .mark .parametrize (
179196 "content" , [None , "invalid-magic" , "invalid-class" , "invalid-data" , "too-short" ]
180197)
181- def test_parse_elf_bad_executable (content ) -> None :
198+ def test_parse_elf_bad_executable (content : str | None ) -> None :
199+ path_str : str | None
182200 if content :
183201 path = pathlib .Path (__file__ ).parent / "manylinux" / f"hello-world-{ content } "
184- path = os .fsdecode (path )
202+ path_str = os .fsdecode (path )
185203 else :
186- path = None
187- with _parse_elf (path ) as ef :
204+ path_str = None
205+ # None is not supported in the type annotation, but it was tested before.
206+ with _parse_elf (path_str ) as ef : # type: ignore[arg-type]
188207 assert ef is None
0 commit comments