1111from typing import TYPE_CHECKING
1212
1313from codeflash .code_utils .normalizers import get_normalizer
14+ from codeflash .languages import current_language , is_python
1415
1516if TYPE_CHECKING :
1617 pass
@@ -20,7 +21,7 @@ def normalize_code(
2021 code : str ,
2122 remove_docstrings : bool = True , # noqa: FBT001, FBT002
2223 return_ast_dump : bool = False , # noqa: FBT001, FBT002
23- language : str = "python" ,
24+ language : str | None = None ,
2425) -> str :
2526 """Normalize code by parsing, cleaning, and normalizing variable names.
2627
@@ -30,16 +31,19 @@ def normalize_code(
3031 code: Source code as string
3132 remove_docstrings: Whether to remove docstrings (Python only)
3233 return_ast_dump: Return AST dump instead of unparsed code (Python only)
33- language: Language of the code ('python', 'javascript', 'typescript')
34+ language: Language of the code. If None, uses the current session language.
3435
3536 Returns:
3637 Normalized code as string
3738 """
39+ if language is None :
40+ language = current_language ().value
41+
3842 try :
3943 normalizer = get_normalizer (language )
4044
4145 # Python has additional options
42- if language == "python" :
46+ if is_python () :
4347 if return_ast_dump :
4448 return normalizer .normalize_for_hash (code )
4549 return normalizer .normalize (code , remove_docstrings = remove_docstrings )
@@ -51,7 +55,7 @@ def normalize_code(
5155 return _basic_normalize (code )
5256 except Exception :
5357 # Parsing error - try other languages or fall back
54- if language == "python" :
58+ if is_python () :
5559 # Try JavaScript as fallback
5660 try :
5761 js_normalizer = get_normalizer ("javascript" )
@@ -76,16 +80,19 @@ def _basic_normalize(code: str) -> str:
7680 return " " .join (code .split ())
7781
7882
79- def get_code_fingerprint (code : str , language : str = "python" ) -> str :
83+ def get_code_fingerprint (code : str , language : str | None = None ) -> str :
8084 """Generate a fingerprint for normalized code.
8185
8286 Args:
8387 code: Source code
84- language: Language of the code ('python', 'javascript', 'typescript')
88+ language: Language of the code. If None, uses the current session language.
8589
8690 Returns:
8791 SHA-256 hash of normalized code
8892 """
93+ if language is None :
94+ language = current_language ().value
95+
8996 try :
9097 normalizer = get_normalizer (language )
9198 return normalizer .get_fingerprint (code )
@@ -95,17 +102,20 @@ def get_code_fingerprint(code: str, language: str = "python") -> str:
95102 return hashlib .sha256 (normalized .encode ()).hexdigest ()
96103
97104
98- def are_codes_duplicate (code1 : str , code2 : str , language : str = "python" ) -> bool :
105+ def are_codes_duplicate (code1 : str , code2 : str , language : str | None = None ) -> bool :
99106 """Check if two code segments are duplicates after normalization.
100107
101108 Args:
102109 code1: First code segment
103110 code2: Second code segment
104- language: Language of the code ('python', 'javascript', 'typescript')
111+ language: Language of the code. If None, uses the current session language.
105112
106113 Returns:
107114 True if codes are structurally identical (ignoring local variable names)
108115 """
116+ if language is None :
117+ language = current_language ().value
118+
109119 try :
110120 normalizer = get_normalizer (language )
111121 return normalizer .are_duplicates (code1 , code2 )
0 commit comments