()
| 192 | |
| 193 | |
| 194 | def test_fixup_module_metadata() -> None: |
| 195 | # Ignores modules not in the trio.X tree. |
| 196 | non_trio_module = types.ModuleType("not_trio") |
| 197 | non_trio_module.some_func = lambda: None # type: ignore[attr-defined] |
| 198 | non_trio_module.some_func.__name__ = "some_func" |
| 199 | non_trio_module.some_func.__qualname__ = "some_func" |
| 200 | |
| 201 | fixup_module_metadata(non_trio_module.__name__, vars(non_trio_module)) |
| 202 | |
| 203 | assert non_trio_module.some_func.__name__ == "some_func" |
| 204 | assert non_trio_module.some_func.__qualname__ == "some_func" |
| 205 | |
| 206 | # Bulild up a fake module to test. Just use lambdas since all we care about is the names. |
| 207 | mod = types.ModuleType("trio._somemodule_impl") |
| 208 | mod.some_func = lambda: None # type: ignore[attr-defined] |
| 209 | mod.some_func.__name__ = "_something_else" |
| 210 | mod.some_func.__qualname__ = "_something_else" |
| 211 | |
| 212 | # No __module__ means it's unchanged. |
| 213 | mod.not_funclike = types.SimpleNamespace() # type: ignore[attr-defined] |
| 214 | mod.not_funclike.__name__ = "not_funclike" |
| 215 | |
| 216 | # Check __qualname__ being absent works. |
| 217 | mod.only_has_name = types.SimpleNamespace() # type: ignore[attr-defined] |
| 218 | mod.only_has_name.__module__ = "trio._somemodule_impl" |
| 219 | mod.only_has_name.__name__ = "only_name" |
| 220 | |
| 221 | # Underscored names are unchanged. |
| 222 | mod._private = lambda: None # type: ignore[attr-defined] |
| 223 | mod._private.__module__ = "trio._somemodule_impl" |
| 224 | mod._private.__name__ = mod._private.__qualname__ = "_private" |
| 225 | |
| 226 | # We recurse into classes. |
| 227 | mod.SomeClass = type( # type: ignore[attr-defined] |
| 228 | "SomeClass", |
| 229 | (), |
| 230 | { |
| 231 | "__init__": lambda self: None, |
| 232 | "method": lambda self: None, |
| 233 | }, |
| 234 | ) |
| 235 | # Reference loop is fine. |
| 236 | mod.SomeClass.recursion = mod.SomeClass |
| 237 | |
| 238 | fixup_module_metadata("trio.somemodule", vars(mod)) |
| 239 | assert mod.some_func.__name__ == "some_func" |
| 240 | assert mod.some_func.__module__ == "trio.somemodule" |
| 241 | assert mod.some_func.__qualname__ == "some_func" |
| 242 | |
| 243 | assert mod.not_funclike.__name__ == "not_funclike" |
| 244 | assert mod._private.__name__ == "_private" |
| 245 | assert mod._private.__module__ == "trio._somemodule_impl" |
| 246 | assert mod._private.__qualname__ == "_private" |
| 247 | |
| 248 | assert mod.only_has_name.__name__ == "only_has_name" |
| 249 | assert mod.only_has_name.__module__ == "trio.somemodule" |
| 250 | assert not hasattr(mod.only_has_name, "__qualname__") |
| 251 |
nothing calls this directly
no test coverage detected
searching dependent graphs…