Skip to content

docs: Fix warnings in re pattern and update py-gfm version#7234

Merged
KevinOConnor merged 1 commit intoKlipper3d:masterfrom
junan76:fix-docs
Apr 1, 2026
Merged

docs: Fix warnings in re pattern and update py-gfm version#7234
KevinOConnor merged 1 commit intoKlipper3d:masterfrom
junan76:fix-docs

Conversation

@junan76
Copy link
Copy Markdown
Contributor

@junan76 junan76 commented Mar 23, 2026

I got the following errors when build the documentations with python3.12 :

/home/junan/Documents/github/klipper/docs/_klipper3d/mkdocs_hooks.py:24: SyntaxWarning: invalid escape sequence '\s'
  len(re.findall("\s*[`]{3,}", line_out))) % 2
/home/junan/Documents/github/klipper/docs/_klipper3d/mkdocs_hooks.py:28: SyntaxWarning: invalid escape sequence '\s'
  line_out = re.sub("\\\s*$", "<br>", line_out)
ERROR    -  Config value: 'markdown_extensions'. Error: global flags not at the start of the expression at position 6
Aborted with 1 Configuration Errors!

what I do to fix these:

  1. Fix re patterns in mkdocs_hooks.py with r prefix;
  2. Update py-gfm versions based on python_version;

@junan76
Copy link
Copy Markdown
Contributor Author

junan76 commented Mar 26, 2026

Hey guys,

I found that github actions workflow currently uses Python 3.8, which silently accepts
invalid escape sequences in regex patterns (e.g., "\s"). However, Python 3.12+
correctly warns about these, causing build failures for users who build the docs
locally with a modern Python version. This change uses raw strings to ensure
compatibility across all Python versions.

@nefelim4ag nefelim4ag added dependencies Pull requests that update a dependency file python Pull requests that update python code labels Mar 26, 2026
Copy link
Copy Markdown
Collaborator

@nefelim4ag nefelim4ag left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems to fix the local build of the docs for me.

Python 3.14.3

Thanks,
-Timofey

@KevinOConnor
Copy link
Copy Markdown
Collaborator

Thanks. We can certainly add the r to the strings. Why has the py-gfm library version changed?

In general, I would prefer to avoid conditional dependencies where possible, as they can make it hard to reproduce the same results across different machines.

-Kevin

@junan76
Copy link
Copy Markdown
Contributor Author

junan76 commented Mar 28, 2026

Thanks. We can certainly add the r to the strings. Why has the py-gfm library version changed?

In general, I would prefer to avoid conditional dependencies where possible, as they can make it hard to reproduce the same results across different machines.

-Kevin

@KevinOConnor

Hi Kevin,

1) With py-gfm version 1.0.2 and Python 3.12+ it reports an error when build docs:

ERROR    -  Config value: 'markdown_extensions'. Error: global flags not at the start of the expression at position 6
Aborted with 1 Configuration Errors!

I found the same error report here in klipper discourse.

2) The root reason:

The gfm library 1.0.2 uses a regex pattern that begins with (?i) to enable case-insensitive matching:

URL_RE = r"(?i)\b((?:(?:ftp|https?)://|www\d{0,3}[.])..."

When this pattern is registered with the Markdown library, inlinepatterns.py wraps it inside another regex:

self.compiled_re = re.compile(r"^(.?)%s(.)$" % pattern, re.DOTALL | re.UNICODE)

This produces:

^(.*?)(?i)\b((?:...

The problem: Python 3.14 enforces that inline flags like (?i) must appear at the very start of the entire regex pattern. Now that ^(.*?) precedes (?i), the flag is no longer at the start, triggering the error.

We can see the diff of gfm/autolink.py between 1.0.2 and 2.0.0:

junan@fedora:~/Desktop/py-gfm$ git diff 1.0.2 2.0.0 -- gfm/autolink.py
diff --git a/gfm/autolink.py b/gfm/autolink.py
index efbdff8..5b150ca 100644
--- a/gfm/autolink.py
+++ b/gfm/autolink.py
@@ -41,20 +41,21 @@ Typical usage
 
 import re
 import markdown
+import xml.etree.ElementTree as etree
 
 URL_RE = (
-    r"(?i)\b((?:(?:ftp|https?)://|www\d{0,3}[.])(?:[^\s()<>]+|"
+    r"\b((?:(?i:ftp|https?)://|(?i:www)\d{0,3}[.])(?:[^\s()<>]+|"
     r"\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()"
     r"<>]+\)))*\)|[^\s`!()\[\]{};:" + r"'" + r'".,<>?«»“”‘’]))'
 )
-PROTOCOL_RE = re.compile(r"^(ftp|https?)://", re.IGNORECASE)
+PROTOCOL_RE = re.compile(r"^(?i:ftp|https?)://")
 
 
 # We can't re-use the built-in AutolinkPattern because we need to add protocols
 # to links without them.
 class AutolinkPattern(markdown.inlinepatterns.Pattern):
     def handleMatch(self, m):
-        el = markdown.util.etree.Element("a")
+        el = etree.Element("a")
 
         href = m.group(2)
         if not PROTOCOL_RE.match(href):

The leading (?i) is removed in version 2.0.0

Thanks,
-Ju Nan

@KevinOConnor
Copy link
Copy Markdown
Collaborator

Thanks. It's also fine if we need to update to a later version of py-gfm, but I'd prefer to avoid conditional versioning. Is there a version of py-gfm that works on both python 3.8 and python 3.12?

-Kevin

@nefelim4ag
Copy link
Copy Markdown
Collaborator

https://pypi.org/project/py-gfm/2.0.0/ - seems to support 3.8+

-Timofey

1. wipe out syntaxwarnings of "invalid escape sequence '\s'"
2. update py-gfm version to fit python3.8+

Signed-off-by: Ju Nan <junan76@163.com>
@junan76
Copy link
Copy Markdown
Contributor Author

junan76 commented Apr 1, 2026

Hi,

@KevinOConnor as Timofey said:

https://pypi.org/project/py-gfm/2.0.0/ - seems to support 3.8+

-Timofey

Thanks Timofey, I tested py-gfm 2.0.0 with python 3.8, it works. And the commit is updated now 😄

-Ju Nan

Screenshot from 2026-04-01 10-26-47

@junan76 junan76 requested a review from nefelim4ag April 1, 2026 02:53
@KevinOConnor KevinOConnor merged commit b0e6ca4 into Klipper3d:master Apr 1, 2026
1 check passed
@KevinOConnor
Copy link
Copy Markdown
Collaborator

Thanks.

-Kevin

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

dependencies Pull requests that update a dependency file python Pull requests that update python code

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants