Skip to content

New-style classes for python2.7 #2147

@nolar

Description

@nolar

The problem:

Pytest uses old-style classes everywhere. E.g.,

class MarkGenerator:
    ………

class MarkDecorator:
    ………

This causes unexpected behaviour in some cases. For example, when trying to get an item from a mark, AttributeError is raised (as in the old-style classes) instead of TypeError (as in the new-style classes).

The old-style classes are long deprecated, and the modern libraries & young developers do not expect this weird behaviour in their code anymore. This causes errors when trying to integrate pytest with other libraries & frameworks. For instance, in Jinja template engine (pallets/jinja#631); but there may be other cases.

The solution:

It would be nice if all classes would be new-style classes, i.e. explicitly inheriting from object:

class MarkGenerator(object):
    ………

class MarkDecorator(object):
    ………

Possible problems:

This will probably break the compatibility with python < 2.7 (officially 2.6+ is declared for pytest).

Also, in python3, the no-braces syntax is a new normal (again), but causes the new-style classes with implicit inheritance from object. The 2.7's new-style syntax is supported.

So, explicit mentioning of (object) may be of use for python2.7 only. Hence, it should be discussed is it worth changing the code for this case.

Notes:

Python 2.7.10.
pytest==3.0.3

Metadata

Metadata

Assignees

No one assigned

    Labels

    good first issueeasy issue that is friendly to new contributortype: backward compatibilitymight present some backward compatibility issues which should be carefully noted in the changelogtype: feature-branchnew feature or API change, should be merged into features branchtype: removalmarks the actual removal of a feature, usually done in major releases

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions