Skip to content

SyntaxWarning: invalid escape sequences (Python 3.12+) #1376

@ghilesmeddour

Description

@ghilesmeddour

When importing and using dowhy on Python 3.12+ (specifically on Windows), several SyntaxWarning messages are triggered regarding invalid escape sequences.

These warnings appear because some strings (mostly in docstrings or regex patterns) contain backslashes (like \s, \i, or \.) that are not recognized as valid Python escape sequences. Starting with Python 3.12, these now trigger a warning and will eventually become a SyntaxError in future versions.

Environment

  • OS: Windows
  • Python Version: 3.14
  • Library Version: 0.14

Steps to Reproduce

Simply importing the library or running a basic causal model triggers the warnings in the console:

import dowhy.datasets

# Load some sample data
data = dowhy.datasets.linear_dataset(
    beta=10,
    num_common_causes=5,
    num_instruments=2,
    num_samples=10000,
    treatment_is_binary=True,
)

Output

.venv\Lib\site-packages\dowhy\graph.py:492: SyntaxWarning: "\s" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\s"? A raw string is also an option.
  elif re.match(".*graph\s*\[.*\]\s*", graph_str):
.venv\Lib\site-packages\dowhy\causal_graph.py:101: SyntaxWarning: "\s" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\s"? A raw string is also an option.
  elif isinstance(graph, str) and re.match(".*graph\s*\[.*\]\s*", graph):
.venv\Lib\site-packages\dowhy\gcm\divergence.py:110: SyntaxWarning: "\i" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\i"? A raw string is also an option.
  D_f(P || Q) = \int f(p(x)/q(x)) q(x) dx ~= -1/N \sum_x log(p(Y = 1 | x) / (1 - p(Y = 1 | x)))
.venv\Lib\site-packages\causallearn\utils\KCI\KCI.py:500: SyntaxWarning: "\e" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\e"? A raw string is also an option.
  With this we could save one repeated calculation of pinv(Kzy+\epsilonI), which consumes most time.
.venv\Lib\site-packages\causallearn\utils\KCI\Kernel.py:45: SyntaxWarning: "\e" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\e"? A raw string is also an option.
  Centers the kernel matrix via a centering matrix R=I-Kz(Kz+\epsilonI)^{-1} and returns RKR
.venv\Lib\site-packages\dowhy\gcm\distribution_change_robust.py:233: SyntaxWarning: "\i" is an invalid escape sequence. Such sequences will not work in the future. Did you mean "\\i"? A raw string is also an option.

Suggested Fix

The affected strings should be converted to raw strings by adding an r prefix.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions