Skip to content

WIP hack around mark object [skip ci] #6410

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

Closed
wants to merge 1 commit into from

Conversation

nicoddemus
Copy link
Member

Hey @RonnyPfannschmidt,

The topic of mark objects came up at work again, and I decided to be a bit of hacking to see how far I could get based on the discussion in #5424.

I basically removed some checks and made MarkDecorator callable, so this is possible:

import attr

import pytest


@attr.s
class MyMark:
    name = attr.ib(type=str)
    x = attr.ib(type=int)


@pytest.mark(MyMark('mymark', 10))
def test(request):
    m = request.node.get_closest_marker(name='mymark')
    assert isinstance(MyMark)
    assert m.x == 10

No registration necessary, just a plain object with the only restriction that it needs to have a name: str attribute (which can also be lifted easily).

Wanted to share because I'm excited that this was possible with so little work (thanks to all the previous work you've done here of course).

Copy link
Member

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Choose a reason for hiding this comment

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

lovely start, i believe we mainly need tests and a good setup for the unpacking/repacking

we might want to add a keyword only parameter called instance_of for the iteration functions

if not isinstance(mark, Mark):
raise TypeError("got {!r} instead of Mark".format(mark))
return [x for x in extracted if isinstance(x, Mark)]
return [getattr(mark, "mark", mark) for mark in mark_list] # unpack MarkDecorator
Copy link
Member

Choose a reason for hiding this comment

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

for safety, this should only extract markdecorators

@nicoddemus nicoddemus added this to the 5.5 milestone Jan 8, 2020
@nicoddemus
Copy link
Member Author

nicoddemus commented Jan 8, 2020

Note: combination should be diallowed, so this:

pytest.mark(obj)(1)

Should raise a `TypeError.

@bluetech

This comment has been minimized.

@nicoddemus nicoddemus changed the base branch from features to master February 13, 2020 01:57
@nicoddemus nicoddemus removed this from the 5.5 milestone Jun 8, 2020
Base automatically changed from master to main March 9, 2021 20:40
@nicoddemus
Copy link
Member Author

Closing for now as this was mostly an experiment. I have no plans to continue working on this on the near term, but of course others are free to use this as basis to implement the whole thing! 👍

@nicoddemus nicoddemus closed this Jun 28, 2021
@nicoddemus nicoddemus deleted the wip-mark-obj branch June 28, 2021 18:40
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.

3 participants