Repository: huggingface/transformers
Pull request: Use regex defailed flags

# What does this PR do?

This PR uses full names of regex flags for better code readability.

Changed files:
filename: src/transformers/models/fsmt/convert_fsmt_original_pytorch_checkpoint_to_pytorch.py
patch: @@ -155,7 +155,7 @@ def convert_fsmt_checkpoint_to_pytorch(fsmt_checkpoint_path, pytorch_dump_folder
             break
     with open(fsmt_merges_file, encoding="utf-8") as fin:
         merges = fin.read()
-    merges = re.sub(r" \d+$", "", merges, 0, re.M)  # remove frequency number
+    merges = re.sub(r" \d+$", "", merges, 0, re.MULTILINE)  # remove frequency number
     print(f"Generating {merges_file}")
     with open(merges_file, "w", encoding="utf-8") as fout:
         fout.write(merges)

filename: src/transformers/models/nougat/tokenization_nougat_fast.py
patch: @@ -68,15 +68,15 @@ def markdown_compatible(text: str) -> str:
     """
     # equation tag
     # Replace lines that start with a pattern like (decimal) \[some text\] with \[[some text] \tag{decimal}\].
-    text = re.sub(r"^\(([\d.]+[a-zA-Z]?)\) \\\[(.+?)\\\]$", r"\[\2 \\tag{\1}\]", text, flags=re.M)
+    text = re.sub(r"^\(([\d.]+[a-zA-Z]?)\) \\\[(.+?)\\\]$", r"\[\2 \\tag{\1}\]", text, flags=re.MULTILINE)
     # Replace lines that start with a pattern like \[some text\] (decimal)  with \[[some text] \tag{decimal}\].
-    text = re.sub(r"^\\\[(.+?)\\\] \(([\d.]+[a-zA-Z]?)\)$", r"\[\1 \\tag{\2}\]", text, flags=re.M)
+    text = re.sub(r"^\\\[(.+?)\\\] \(([\d.]+[a-zA-Z]?)\)$", r"\[\1 \\tag{\2}\]", text, flags=re.MULTILINE)
     # Replace lines that start with a pattern like \[some text\] (digits) \[another text\]  with \[[some text] \tag{digits}\] [another text].
     text = re.sub(
         r"^\\\[(.+?)\\\] \(([\d.]+[a-zA-Z]?)\) (\\\[.+?\\\])$",
         r"\[\1 \\tag{\2}\] \3",
         text,
-        flags=re.M,
+        flags=re.MULTILINE,
     )
     # multi line
     text = text.replace(r"\. ", ". ")
@@ -90,7 +90,7 @@ def markdown_compatible(text: str) -> str:
         text,
     )
     # algorithms
-    text = re.sub(r"```\s*(.+?)\s*```", r"```\n\1\n```", text, flags=re.S)
+    text = re.sub(r"```\s*(.+?)\s*```", r"```\n\1\n```", text, flags=re.DOTALL)
 
     return text
 
@@ -131,7 +131,7 @@ def normalize_list_like_lines(generation):
             if not rest:
                 continue
             # Infer current nesting level based on detected numbering
-            if re.match(r"^[\dixv]+((?:\.[\dixv])?)+$", potential_numeral, flags=re.I | re.M):
+            if re.match(r"^[\dixv]+((?:\.[\dixv])?)+$", potential_numeral, flags=re.IGNORECASE | re.MULTILINE):
                 level = potential_numeral.count(".")
 
             replacement += (
@@ -477,7 +477,7 @@ def correct_tables(self, generation: str) -> str:
         generation = generation.replace("\\end{tabular} \\end{table}", "\\end{tabular}\n\\end{table}")
         generation = generation.replace("\\end{table} Tab", "\\end{table}\nTab")
 
-        generation = re.sub(r"(^.+)\\begin{tab", r"\1\n\\begin{tab", generation, flags=re.M)
+        generation = re.sub(r"(^.+)\\begin{tab", r"\1\n\\begin{tab", generation, flags=re.MULTILINE)
 
         # Remove left-aligned empty LaTeX tabular blocks.
         generation = generation.replace(r"\begin{tabular}{l l}  & \\ \end{tabular}", "")
@@ -505,7 +505,7 @@ def post_process_single(self, generation: str, fix_markdown: bool = True) -> str
         generation = generation.replace("\n* [leftmargin=*]\n", "\n")
         # Remove lines with markdown headings starting with #, with numerals,
         # and possibly roman numerals with trailing spaces and newlines
-        generation = re.sub(r"^#+ (?:[\d+\.]+|[ixv\.]+)?\s*(?:$|\n\s*)", "", generation, flags=re.M)
+        generation = re.sub(r"^#+ (?:[\d+\.]+|[ixv\.]+)?\s*(?:$|\n\s*)", "", generation, flags=re.MULTILINE)
         # most likely hallucinated titles
         lines = generation.split("\n")
         if lines[-1].startswith("#") and lines[-1].lstrip("#").startswith(" ") and len(lines) > 1:
@@ -516,9 +516,9 @@ def post_process_single(self, generation: str, fix_markdown: bool = True) -> str
         # Reference corrections
         generation = self.remove_hallucinated_references(generation)
         # Remove lines starting with asterisks and numbers like "*[1]" and followed by capital letters and periods (ie too long references)
-        generation = re.sub(r"^\* \[\d+\](\s?[A-W]\.+\s?){10,}.*$", "", generation, flags=re.M)
+        generation = re.sub(r"^\* \[\d+\](\s?[A-W]\.+\s?){10,}.*$", "", generation, flags=re.MULTILINE)
         # Remove empty brackets after a reference number in brackets. *[12][]ABC will become *[12]ABC
-        generation = re.sub(r"^(\* \[\d+\])\[\](.*)$", r"\1\2", generation, flags=re.M)
+        generation = re.sub(r"^(\* \[\d+\])\[\](.*)$", r"\1\2", generation, flags=re.MULTILINE)
         # Remove single characters before or after 2 new lines
         generation = re.sub(r"(^\w\n\n|\n\n\w$)", "", generation)
         # pmc math artifact correction
@@ -570,9 +570,9 @@ def post_process_single(self, generation: str, fix_markdown: bool = True) -> str
         # Remove lines containing "S.A.B." one or more times. Was included in Nougat's code.
         generation = re.sub(r"(\*\*S\. A\. B\.\*\*\n+){2,}", "", generation)
         # Remove markdown-style headers that are incomplete or empty on multiple lines.
-        generation = re.sub(r"^#+( [\[\d\w])?$", "", generation, flags=re.M)
+        generation = re.sub(r"^#+( [\[\d\w])?$", "", generation, flags=re.MULTILINE)
         # Remove lines with just one period.
-        generation = re.sub(r"^\.\s*$", "", generation, flags=re.M)
+        generation = re.sub(r"^\.\s*$", "", generation, flags=re.MULTILINE)
         # Replace instances of three or more newlines with just two newlines.
         generation = re.sub(r"\n{3,}", "\n\n", generation)
         if fix_markdown:

filename: src/transformers/testing_utils.py
patch: @@ -1599,7 +1599,7 @@ def evaluate_side_effect_factory(
 # final message
 # it can handle a single string or a multiline buffer
 def apply_print_resets(buf):
-    return re.sub(r"^.*\r", "", buf, 0, re.M)
+    return re.sub(r"^.*\r", "", buf, 0, re.MULTILINE)
 
 
 def assert_screenout(out, what):
@@ -2316,7 +2316,7 @@ def summary_failures_short(tr):
             msg = tr._getfailureheadline(rep)
             tr.write_sep("_", msg, red=True, bold=True)
             # chop off the optional leading extra frames, leaving only the last one
-            longrepr = re.sub(r".*_ _ _ (_ ){10,}_ _ ", "", rep.longreprtext, 0, re.M | re.S)
+            longrepr = re.sub(r".*_ _ _ (_ ){10,}_ _ ", "", rep.longreprtext, 0, re.MULTILINE | re.DOTALL)
             tr._tw.line(longrepr)
             # note: not printing out any rep.sections to keep the report short
 
@@ -2464,7 +2464,7 @@ def pytest_xdist_worker_id():
     if `-n 1` or `pytest-xdist` isn't being used.
     """
     worker = os.environ.get("PYTEST_XDIST_WORKER", "gw0")
-    worker = re.sub(r"^gw", "", worker, 0, re.M)
+    worker = re.sub(r"^gw", "", worker, 0, re.MULTILINE)
     return int(worker)