src/upgrade_type_hints/update.py
def update_file(
    filename: str,
    futures: bool,
    native_types: list,
    imported_types: list,
    imports_to_delete: list,
    futures_import_found: bool,
    future_import_insert_position: int,
) -> None:
    """
    Reads a file, removes imports and updates types, then writes back to it.
    """
    with open(filename, mode='rb+') as file:
        content = file.readlines()

    for item in native_types:
        # New native types don't require us to do anything except inplace-replacement of type hints
        content[item['line_number'] - 1] = replace_type(
            item['annotation'], item['new_annotation'], content[item['line_number'] - 1]
        )

    new_import_statements: list[bytes] = []
    for item in imported_types:
        # The types handled here require us to add a new import statement at
        # the top of the file in addition to the inplace replacement of type hints
        content[item['line_number'] - 1] = replace_type(
            item['annotation'], item['new_annotation'], content[item['line_number'] - 1]
        )
        new_import = f'from {item["import_from"]} import {item["new_annotation"]}\n'.encode(encoding='utf-8')
        if new_import not in content:
            new_import_statements.append(new_import)

    # Filter out repeated imports
    new_import_statements = list(set(new_import_statements))
    if futures and not futures_import_found:
        new_import_statements.insert(0, b'from __future__ import annotations\n\n')

    # Remove old imports
    for operation in imports_to_delete:
        content = remove_import(operation, content)

    if future_import_insert_position > 0:
        content = (
            content[:future_import_insert_position]
            + new_import_statements
            + content[future_import_insert_position:]
        )
    else:
        content = new_import_statements + content
    with open(filename, 'wb') as file:
        file.writelines(content)

    Reads a file, removes imports and updates types, then writes back to it.
    
Reads a file, removes imports and updates types, then writes back to it.
Reads a file removes imports and updates types then writes back to it .
def update_file(
    filename: str,
    futures: bool,
    native_types: list,
    imported_types: list,
    imports_to_delete: list,
    futures_import_found: bool,
    future_import_insert_position: int,
) -> None:
    with open(filename, mode='rb+') as file:
        content = file.readlines()
    for item in native_types:
        content[item['line_number'] - 1] = replace_type(
            item['annotation'], item['new_annotation'], content[item['line_number'] - 1]
        )
    new_import_statements: list[bytes] = []
    for item in imported_types:
        content[item['line_number'] - 1] = replace_type(
            item['annotation'], item['new_annotation'], content[item['line_number'] - 1]
        )
        new_import = f'from {item["import_from"]} import {item["new_annotation"]}\n'.encode(encoding='utf-8')
        if new_import not in content:
            new_import_statements.append(new_import)
    new_import_statements = list(set(new_import_statements))
    if futures and not futures_import_found:
        new_import_statements.insert(0, b'from __future__ import annotations\n\n')
    for operation in imports_to_delete:
        content = remove_import(operation, content)
    if future_import_insert_position > 0:
        content = (
            content[:future_import_insert_position]
            + new_import_statements
            + content[future_import_insert_position:]
        )
    else:
        content = new_import_statements + content
    with open(filename, 'wb') as file:
        file.writelines(content)
def update_file ( filename : str , futures : bool , native_types : list , imported_types : list , imports_to_delete : list , futures_import_found : bool , future_import_insert_position : int , ) -> None : with open ( filename , mode = 'rb+' ) as file : content = file . readlines ( ) for item in native_types : content [ item [ 'line_number' ] - 1 ] = replace_type ( item [ 'annotation' ] , item [ 'new_annotation' ] , content [ item [ 'line_number' ] - 1 ] ) new_import_statements : list [ bytes ] = [ ] for item in imported_types : content [ item [ 'line_number' ] - 1 ] = replace_type ( item [ 'annotation' ] , item [ 'new_annotation' ] , content [ item [ 'line_number' ] - 1 ] ) new_import = f'from {item["import_from"]} import {item["new_annotation"]}\n' . encode ( encoding = 'utf-8' ) if new_import not in content : new_import_statements . append ( new_import ) new_import_statements = list ( set ( new_import_statements ) ) if futures and not futures_import_found : new_import_statements . insert ( 0 , b'from __future__ import annotations\n\n' ) for operation in imports_to_delete : content = remove_import ( operation , content ) if future_import_insert_position > 0 : content = ( content [ : future_import_insert_position ] + new_import_statements + content [ future_import_insert_position : ] ) else : content = new_import_statements + content with open ( filename , 'wb' ) as file : file . writelines ( content )
Reads a file, removes imports and updates types, then writes back to it.
Reads a file removes imports and updates types then writes back to it .
"""
    Reads a file, removes imports and updates types, then writes back to it.
    """ # New native types don't require us to do anything except inplace-replacement of type hints # The types handled here require us to add a new import statement at # the top of the file in addition to the inplace replacement of type hints # Filter out repeated imports # Remove old imports
filename
str
futures
bool
native_types
list
imported_types
list
imports_to_delete
list
futures_import_found
bool
future_import_insert_position
int