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

is_dataclass returns True if getattr always succeeds. #82049

Closed
JohanHidding mannequin opened this issue Aug 15, 2019 · 7 comments
Closed

is_dataclass returns True if getattr always succeeds. #82049

JohanHidding mannequin opened this issue Aug 15, 2019 · 7 comments
Assignees
Labels
3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error

Comments

@JohanHidding
Copy link
Mannequin

JohanHidding mannequin commented Aug 15, 2019

BPO 37868
Nosy @ericvsmith, @serhiy-storchaka, @miss-islington
PRs
  • bpo-37868: Improve is_dataclass for instances. #15325
  • [3.8] bpo-37868: Improve is_dataclass for instances. (GH-15325) #15339
  • [3.7] bpo-37868: Improve is_dataclass for instances. (GH-15325) #15340
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = 'https://github.com/ericvsmith'
    closed_at = <Date 2019-08-20.06:05:07.986>
    created_at = <Date 2019-08-15.12:02:15.512>
    labels = ['3.7', 'type-bug', 'library']
    title = '`is_dataclass` returns `True` if `getattr` always succeeds.'
    updated_at = <Date 2019-08-20.06:05:07.985>
    user = 'https://bugs.python.org/JohanHidding'

    bugs.python.org fields:

    activity = <Date 2019-08-20.06:05:07.985>
    actor = 'eric.smith'
    assignee = 'eric.smith'
    closed = True
    closed_date = <Date 2019-08-20.06:05:07.986>
    closer = 'eric.smith'
    components = ['Library (Lib)']
    creation = <Date 2019-08-15.12:02:15.512>
    creator = 'Johan Hidding'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 37868
    keywords = ['patch']
    message_count = 7.0
    messages = ['349802', '349821', '349822', '349832', '349989', '349990', '349991']
    nosy_count = 4.0
    nosy_names = ['eric.smith', 'serhiy.storchaka', 'miss-islington', 'Johan Hidding']
    pr_nums = ['15325', '15339', '15340']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue37868'
    versions = ['Python 3.7']

    @JohanHidding
    Copy link
    Mannequin Author

    JohanHidding mannequin commented Aug 15, 2019

    Given a class A that overloads __getattr__

    class A:
      def __getattr__(self, key):
        return 0
    

    An instance of this class is always identified as a dataclass.

    from dataclasses import is_dataclass
    
    a = A()
    print(is_dataclass(a))
    

    gives the output True.

    Possible fix: check for the instance type.

    is_dataclass(type(a))
    

    does give the correct answer.

    @JohanHidding JohanHidding mannequin added 3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error labels Aug 15, 2019
    @ericvsmith ericvsmith self-assigned this Aug 15, 2019
    @ericvsmith
    Copy link
    Member

    I'm guessing I'm looking up the attribute on the instance, not the class.

    @serhiy-storchaka
    Copy link
    Member

    I am not sure that it is good idea to accept a type and an instance, but if it is a goal, is_dataclass() should be defined as:

    def is_dataclass(obj):
        cls = obj if isinstance(obj, type) else type(obj)
        return hasattr(cls, _FIELDS)

    _is_dataclass_instance() should be changed too:

    def _is_dataclass_instance(obj):
        return hasattr(type(obj), _FIELDS)

    @ericvsmith
    Copy link
    Member

    Yeah, I agree it's not an awesome design to work with classes or instances, but it's documented that way. As soon as I write some tests I'll check this in.

    @ericvsmith
    Copy link
    Member

    New changeset b0f4dab by Eric V. Smith in branch 'master':
    bpo-37868: Improve is_dataclass for instances. (GH-15325)
    b0f4dab

    @miss-islington
    Copy link
    Contributor

    New changeset 1271ee8 by Miss Islington (bot) in branch '3.8':
    bpo-37868: Improve is_dataclass for instances. (GH-15325)
    1271ee8

    @miss-islington
    Copy link
    Contributor

    New changeset 02c1457 by Miss Islington (bot) in branch '3.7':
    bpo-37868: Improve is_dataclass for instances. (GH-15325)
    02c1457

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.7 (EOL) end of life stdlib Python modules in the Lib dir type-bug An unexpected behavior, bug, or error
    Projects
    None yet
    Development

    No branches or pull requests

    3 participants