Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Scanner to re.pyi #3071

Closed
wants to merge 1 commit into from

Conversation

jonathanslenders
Copy link
Contributor

No description provided.

Copy link
Collaborator

@srittau srittau left a comment

Choose a reason for hiding this comment

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

Thanks, left a few comments below.

@@ -153,3 +153,11 @@ def escape(string: AnyStr) -> AnyStr: ...

def purge() -> None: ...
def template(pattern: Union[AnyStr, Pattern[AnyStr]], flags: _FlagsType = ...) -> Pattern[AnyStr]: ...

class Scanner:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please add an undocumented flag. Also, this needs to be generic over AnyStr for the methods to have correct types:

Suggested change
class Scanner:
# undocumented
class Scanner(Generic[AnyStr]):

class Scanner:
def __init__(
self,
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]],
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems the action can be anything, not just a callable:

Suggested change
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]],
lexicon: Sequence[Tuple[AnyStr, Any]],

Alternatively, another TypeVar for the return value might make sense, in which case this would be:

Suggested change
lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]],
lexicon: Sequence[Tuple[AnyStr, Union[None, _T, Callable[[Scanner, AnyStr], Optional[_T]]]],

lexicon: Sequence[Tuple[AnyStr, Callable[[Scanner, AnyStr], Any]]],
flags: _FlagsType = ...,
) -> None: ...
def scan(self, string: AnyStr) -> Tuple[List[Any], AnyStr]: ...
Copy link
Collaborator

Choose a reason for hiding this comment

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

In the case of introducing another type var, List[Any] -> List[_T].

@jonathanslenders
Copy link
Contributor Author

Would this make sense?

# undocumented

_T = TypeVar('_T', bound=AnyStr)
_T2 = TypeVar('_T2')

class Scanner(Generic[_T, _T2]):
    def __init__(
        self,
        lexicon: Sequence[Tuple[_T, Union[None, _T2, Callable[[Scanner, _T], Optional[_T2]]]]],
        flags: _FlagsType = ...,
    ) -> None: ...
    def scan(self, string: _T) -> Tuple[List[_T2], _T]: ...

Should we break it down in smaller pieces?

@srittau
Copy link
Collaborator

srittau commented Jun 19, 2019

Looks good with two changes: The # undocumented should directly precede the class and AnyStr is already a type-var, so instead of _T you can use AnyStr directly (and don't need to list it in Generic).

@jonathanslenders
Copy link
Contributor Author

So, something like this would do it?

_T = TypeVar('_T')

# undocumented

class Scanner(Generic[_T]):
    def __init__(
        self,
        lexicon: Sequence[Tuple[AnyStr, Union[None, _T, Callable[[Scanner, AnyStr], Optional[_T]]]]],
        flags: _FlagsType = ...,
    ) -> None: ...
    def scan(self, string: AnyStr) -> Tuple[List[_T], AnyStr]: ...

What is the reason that AnyStr does not have to be listed in the Generic, while the other type variable should?

@JelleZijlstra
Copy link
Member

What is the reason that AnyStr does not have to be listed in the Generic, while the other type variable should?

In this case you have a generic method on a generic class. The class is generic over _T, and the method is generic over AnyStr. So for example, if you have a Scanner[int], scanner.scan("") would return a Tuple[List[int], str], and scanner.scan(b"") would return a Tuple[List[int], bytes].

@srittau
Copy link
Collaborator

srittau commented Oct 8, 2019

Also, flake8 fails because of white space in line 156. @jonathanslenders Are you still interested in this PR?

@srittau
Copy link
Collaborator

srittau commented Oct 20, 2019

Closing for now, please reopen if you find time to work on this.

@srittau srittau closed this Oct 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants