| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
Sorry, something went wrong.
Adding another copy to be kept in sync is not very appealing, so I would be in favor of checking whether the private module idea can work. (note that typing and annotationlib use a set for the memo instead of a dict, which can potentially lead to issues when ids are reused). |
Sorry, something went wrong.
|
I agree that it's not really appealing to have yet another copy of the unwrap function to keep updated. It's unfortunate inspect is such a heavy import. This is currently nearly always triggered due to dataclasses generating methods before the slotted version of the class is created and __repr__ being wrapped in recursive_repr. As an alternative approach, we could move the creation of the slotted version of the class to before the methods are generated which should prevent basic slotted dataclasses from triggering the import. User methods that are wrapped would still trigger the import but this is probably sufficient in most cases. |
Sorry, something went wrong.
|
I agree that not adding another copy is better. I took @DavidCEllis's advice and changed the order of calling _add_slots, so now inspect is only imported when a slotted dataclass has a user defined method wrapped by a decorator. I think that's a rare case, so we don't need to optimize it. |
Sorry, something went wrong.
|
This PR happens to solve a bug on main: from dataclasses import dataclass
@dataclass(frozen=True, slots=True)
class F:
x: int
def m(self):
return super().__repr__()
f = F(1)
f.y = 2 # main: TypeError: super(type, obj): obj (instance of F) is not an instance ...; PR: FrozenInstanceError
print(f)
print(f.m()) # also affected
Could you add a regression test and update the news entry? |
Sorry, something went wrong.
| Back | FazBrowse Home | New Git URL |
Using @dataclass(slots=True) always imports inspect, just for the single inspect.unwrap() call in _add_slots().
typing and annotationlib already inline the same __wrapped__ walk to avoid the import, so dataclasses can too. Perhaps in the future unwrap() could move into a small private module that inspect and all these copies import, so the implementations do not drift apart.
Importing a module that defines one slotted dataclass, best of 30 runs.
About 2.3x faster.