Deserialize import metadata from bytes into mypy AST nodes.
(import_bytes: bytes)
| 1951 | |
| 1952 | |
| 1953 | def deserialize_imports(import_bytes: bytes) -> list[ImportBase]: |
| 1954 | """Deserialize import metadata from bytes into mypy AST nodes.""" |
| 1955 | if not import_bytes: |
| 1956 | return [] |
| 1957 | |
| 1958 | data = ReadBuffer(import_bytes) |
| 1959 | |
| 1960 | expect_tag(data, LIST_GEN) |
| 1961 | n_imports = read_int_bare(data) |
| 1962 | |
| 1963 | imports: list[ImportBase] = [] |
| 1964 | |
| 1965 | for _ in range(n_imports): |
| 1966 | tag = read_tag(data) |
| 1967 | |
| 1968 | if tag == IMPORT_METADATA: |
| 1969 | name = read_str(data) |
| 1970 | relative = read_int(data) |
| 1971 | |
| 1972 | has_asname = read_bool(data) |
| 1973 | if has_asname: |
| 1974 | asname = read_str(data) |
| 1975 | else: |
| 1976 | asname = None |
| 1977 | |
| 1978 | # Note: relative imports are handled via ImportFrom, so relative should be 0 here |
| 1979 | stmt = Import([(name, asname)]) |
| 1980 | _read_and_set_import_metadata(data, stmt) |
| 1981 | imports.append(stmt) |
| 1982 | |
| 1983 | elif tag == IMPORTFROM_METADATA: |
| 1984 | module = read_str(data) |
| 1985 | relative = read_int(data) |
| 1986 | |
| 1987 | expect_tag(data, LIST_GEN) |
| 1988 | n_names = read_int_bare(data) |
| 1989 | names: list[tuple[str, str | None]] = [] |
| 1990 | |
| 1991 | for _ in range(n_names): |
| 1992 | name = read_str(data) |
| 1993 | has_asname = read_bool(data) |
| 1994 | if has_asname: |
| 1995 | asname = read_str(data) |
| 1996 | else: |
| 1997 | asname = None |
| 1998 | names.append((name, asname)) |
| 1999 | |
| 2000 | stmt = ImportFrom(module, relative, names) |
| 2001 | _read_and_set_import_metadata(data, stmt) |
| 2002 | imports.append(stmt) |
| 2003 | |
| 2004 | elif tag == IMPORTALL_METADATA: |
| 2005 | module = read_str(data) |
| 2006 | relative = read_int(data) |
| 2007 | |
| 2008 | stmt = ImportAll(module, relative) |
| 2009 | _read_and_set_import_metadata(data, stmt) |
| 2010 | imports.append(stmt) |
searching dependent graphs…