-
-
Notifications
You must be signed in to change notification settings - Fork 426
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
fix: Token now pattern matches correctly #1181
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is possible to provide backwards compatibility by not making type
and value
be required positional arguments, but instead use *args
and **kwargs
and have the actual supported signature(s) be only present in @overload
declarations. But I am not sure if this should be done, you are correct that this is more work than I originally thought.
Also, the update
method should also be updated with the changed parameter name.
Fixed, also added setter for |
Please let me know if you think this should be added, I'm happy to figure it out. |
I was thinking we'll just add a new param, The downside is we'll have to set type and value to None too. But it doesn't break anything. Btw, why not just do P.S. we'll also need a deprecation warning for any use of |
That would make the typing inconsistent between The args thing seems to be ok.
Does order of
On it. |
Hey, Otherwise, I would have to manually write all the parsing of Also, this way will allow you to remove the deprecated code in the future easily. One caveat is that the errors will contain one of the wrapped functions names (still should be easily readable), e.g.:
Also |
Also, I'm pretty sure it would be nice to add proper typings to |
Why did you write def __new__(cls, *args, **kwargs):
if "type_" in kwargs:
return cls._deprecated_new(*args, **kwargs)
return cls._future_new(*args, **kwargs) Instead of just - ? def __new__(cls, *args, **kwargs):
if "type_" in kwargs:
kwargs["type"] = kwargs["type_"]
return cls.same_new(*args, **kwargs)
What do you mean?
That's true! We should fix that. P.S. Earlier you wrote about adding a
Can you please explain that a little bit more? |
Both should be fine, but in the first one, errors will be more readable:
makes more sense if you put in something like
It's supposed to be removed, so I wouldn't worry too much about code duplication.
The warnings don't really show until we change the configs: At least they didn't show to me, when I tested this.
Unless we want to allow |
Fixed types and added typings to |
That's a bit of a nitpick. I can easily add this, and get an even better error - def __new__(cls, *args, **kwargs):
if "type_" in kwargs:
if "type" in kwargs:
raise TypeError("Error: using both 'type' and the deprecated 'type_' as arguments.")
kwargs["type"] = kwargs.pop("type_")
return cls.same_new(*args, **kwargs)
That link is from 2014. But when I test it in Python 3.10 - >>> import warnings
>>> warnings.warn('test', DeprecationWarning)
<stdin>:1: DeprecationWarning: test |
I don't know if this is necessarily a better error, as it does nothing to address my use case (which while valid, I admit is really minor). It's just a different error. Nevertheless, I've added it, as I believe it makes a lot of sense here. As I've said, both versions are fine to me, so I've changed to the one you prefer.
That link is 100% valid still. Please look at python docs regarding warnings. The issue can be solved by adding:
to the top of this file, but it breaks the standalone version, which I really don't understand. So, I'm leaving this the way it is. |
Thanks! I see, you're right, the behavior changes for imported modules. I'll look into adding it. |
value if value is not None else self.value, | ||
self | ||
) | ||
|
||
@classmethod | ||
def new_borrow_pos(cls: Type[_T], type_: str, value: Any, borrow_t: 'Token') -> _T: | ||
return cls(type_, value, borrow_t.start_pos, borrow_t.line, borrow_t.column, borrow_t.end_line, borrow_t.end_column, borrow_t.end_pos) | ||
|
||
@property | ||
def type_(self) -> str: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
After thinking about this a bit more: Do we actually need this property for backwards compatibility? The type_
didn't exists before, and adding a attribute that is deprecated-on-release is a bit pointless.
If we decide to keep it, it should raise a DeprecationWarning.
In regards to Deprecation Warnings: They should be silenced by default, if someone cares they need to add the call to |
@marcinplatek Thank you for your contribution! Sorry it took so long to review and merge, |
I've added fixes as per #1180 (comment). I don't think that the backward compatibility is possible in this case, as the original argument is positional, e.g.
Please correct me if I'm missing something.
I've also added a bunch of tests (which only are run if the version is 3.10 or higher).
I wasn't sure how to use assert statements in structural pattern matching, but it does the job.
closes #1180