OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

How do I get a regex pattern type for MyPy

  • Thread starter Thread starter AlexLordThorsen
  • Start date Start date
A

AlexLordThorsen

Guest
If I compile a regex

Code:
>>> type(re.compile(""))
<class '_sre.SRE_Pattern'>

And want to pass that regex to a function and use Mypy to type check

Code:
def my_func(compiled_regex: _sre.SRE_Pattern):

I'm running into this problem

Code:
>>> import _sre
>>> from _sre import SRE_Pattern
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: cannot import name 'SRE_Pattern'

It seems that you can import _sre but for some reason SRE_Pattern isn't importable.
<p>If I compile a regex</p>

<pre><code>>>> type(re.compile(""))
<class '_sre.SRE_Pattern'>
</code></pre>

<p>And want to pass that regex to a function and use Mypy to type check</p>

<pre><code>def my_func(compiled_regex: _sre.SRE_Pattern):
</code></pre>

<p>I'm running into this problem</p>

<pre><code>>>> import _sre
>>> from _sre import SRE_Pattern
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: cannot import name 'SRE_Pattern'
</code></pre>

<p>It seems that you can import <code>_sre</code> but for some reason <code>SRE_Pattern</code> isn't importable.</p>
 

Latest posts

J
Replies
0
Views
1
Jeff P Chacko
J
Top