#🔒 regex scanner type annotation, not working with pyright

94 messages · Page 1 of 1 (latest)

quaint panther
#
import regex
from regex import Scanner

text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)

def print_matches(matches:Scanner[str]):
    for match in matches:
        print(match)

print_matches(matches)

Im trying to add type annotation to my print_matches() function, but i get an error in zed
````"Scanner[str]" is not iterable "iter" method not defined (Pyright reportGeneralTypeIssues)```

How do i fix this, and what is causing the problem?

I also get an error when i run this:

                              ~~~~~~~^^^^^
TypeError: type 'Scanner' is not subscriptable```
winged wadiBOT
#

@quaint panther

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

glacial shard
#

what version python you running

quaint panther
#

Python 3.13.7

glacial shard
#

whats a scanner

#

oh its in the regex module?

quaint panther
#

its the object that returns from the finditer method

glacial shard
#

wait python regex module is re

#

why did you need a third party one

quaint panther
#

no this is regex which is said to be a drop in replacement for re

#

i wanted to do regex pattern searching in reverse

glacial shard
#

link docs

quaint panther
#

now that you say it, i might try to use re module, because they might actully have reverse search feature

jaunty yacht
winged wadiBOT
#

regex_3/_regex_core.py line 4391

class Scanner:```
quaint panther
#
import regex
from regex import Scanner


text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)

def print_matches(matches):
    for match in matches:
        print(match)

print_matches(matches)```

because this runs fine
#

Scanner is iterable, otherwise the for loop wouldend work

glacial shard
#

pls tell us what print(type(matches)) returns

quaint panther
#

<class '_regex.Scanner'>

jaunty yacht
#

I don't think finditer returns a- huh.

quaint panther
#

what?

jaunty yacht
#

I think despite the name, that might be an unrelated class

glacial shard
#

why not just Scanner

#

instead of Scanner[str]

jaunty yacht
#

as in, finditer returns _regex.Scanner which is a class defined only in the compiled extension part of the regex module, and regex.Scanner is an entirely different class

winged wadiBOT
#

regex_3%2F_regex.c lines 612 to 617

typedef struct ScannerObject {
    PyObject_HEAD
    PatternObject* pattern;
    RE_State state;
    int status;
} ScannerObject;```
jaunty yacht
#

yeah, likely

quaint panther
glacial shard
#

whats error for that

quaint panther
#

sec

jaunty yacht
#

Realistically, since no part of this library is typehinted anyway (yikes), just use Iterator[str] as the typehint

glacial shard
jaunty yacht
glacial shard
winged wadiBOT
#

regex_3/regex.py line 732

# Make Pattern public for typing annotations.```
jaunty yacht
quaint panther
# glacial shard whats error for that
import regex
from regex import Scanner

text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)



def print_matches(matches:Scanner):
    for match in matches:
        print(match)

print_matches(matches)
  "__iter__" method not defined (Pyright)```

It runs fine, but i get an error in my editor
minor token
quaint panther
quaint panther
glacial shard
#

use ```py

quaint panther
#

ah

minor token
quaint panther
#

thats a good idea

#
from ast import Match
import regex
from typing import Iterator
text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)



def print_matches(matches:Iterator[Match]):
    for match in matches:
        print(match)

print_matches(matches)```

i get this error ```Argument of type "Scanner[str]" cannot be assigned to parameter "matches" of type "Iterator[Match]" in function "print_matches"
  "Scanner[str]" is incompatible with protocol "Iterator[Match]"
    "__next__" is an incompatible type
      Type "() -> Match[str]" is not assignable to type "() -> _T_co@Iterator"
        Function return type "Match[str]" is incompatible with type "_T_co@Iterator"
          Type "Match[str]" is not assignable to type "Match"
    "__iter__" is an incompatible type
      Type "() -> Scanner[str]" is not assignable to type "() -> Iterator[_T_co@Iterator]"
        Function return type "Scanner[str]" is incompatible with type "Iterator[_T_co@Iterator]"
... (Pyright reportArgumentType)```
#

in editor

glacial shard
# quaint panther thats a good idea

you could make an issue to this project and ask about the slight confusion between having regex.Scanner in the python, but the code returning _regex.Scanner (a different class) from functions, perhaps offering you can type hint it

quaint panther
#

let me first try something like this first: Iterator[Match[AnyStr]]

jaunty yacht
quaint panther
#

wait i think i got it ```py
from regex import Match
import regex
from typing import Iterator
text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)

def print_matches(matches:Iterator[Match[str]]):
for match in matches:
print(match)

print_matches(matches)```

this runs without any errors in the editor and the python interpreter

#

i think its fixed, can even add

type scanner = Iterator[Match[str]]

to make it easier to write

#

well syntax

jaunty yacht
#

You can use regex._regex.Scanner

glacial shard
#

lol

jaunty yacht
#

since typeshed apparently has typehints for it, it works

glacial shard
#

can't have py on the next line

quaint panther
jaunty yacht
quaint panther
#

let me try that

glacial shard
#

definitely isn't

quaint panther
#

like this?```py
def print_matches(matches:regex._regex.Scanner):
for match in matches:
print(match)

jaunty yacht
jaunty yacht
#

and after py, too. probably the reason

glacial shard
quaint panther
#
from regex import Match
import regex
from typing import Iterator
text = " yep "

pattern = regex.compile(r"yep")
matches = pattern.finditer(text)
def print_matches(matches:regex._regex.Scanner):
    for match in matches:
        print(match)

print_matches(matches)

this gives an error "_regex" is not a known attribute of module "regex" (Pyright reportAttributeAccessIssue)

glacial shard
quaint panther
#

im reading it

glacial shard
winged wadiBOT
#

stubs/regex/regex/_regex.pyi line 20

class Scanner(Generic[AnyStr]):```
glacial shard
#

maybe installing that package will let you type it easi[ly/er]

quaint panther
#

well, i don think there is any need to find a different solution since this


from regex import Match
import regex
from typing import Iterator
text = " yep "

pattern = regex.compile(r"yep")

matches = pattern.finditer(text)

type scanner = Iterator[Match[str]]

def print_matches(matches:scanner):
    for match in matches:
        print(match)

print_matches(matches)

works fine, unless im overlooking something

minor token
#

You shouldn't define a type in lower case

glacial shard
#

bruh i can see why static languages gloat sometimes 😅

quaint panther
glacial shard
#

with that package installed, perhaps ```py
type Scanner = regex._regex.Scanner

def print_matches(matches: Scanner):

works
#

the types-regex package should theoretically inform the the type checker that the Scanner object is iterable etc

quaint panther
minor token
#

Maybe RegexIterReturn

quaint panther
#

makes it easy to remember

#

actully that makes more sense RegexIterReturn

quaint panther
#

well since this ```py
type RegexIterReturn = Iterator[Match[str]]

Has solved my problem, im gonne mark this as closed, thanks  everyone for helping me, was tring to solve this for an hour on my own
#

!close

winged wadiBOT
#
Python help channel closed with !close

This help channel has been closed. Feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.