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

escape function does not return Markup class for instances implementing __html__ method #68

Closed
mila opened this issue May 12, 2017 · 5 comments
Milestone

Comments

@mila
Copy link

mila commented May 12, 2017

The escape function does not wrap a return value of __html__ method as Markup.

Expected behaviour documented in README.rst:

>>> class Foo(object):
...  def __html__(self):
...   return '<strong>Nice</strong>'
...
>>> escape(Foo())
Markup(u'<strong>Nice</strong>')

Actual behaviour:

>>> from markupsafe import escape
>>> class Foo(object):
...  def __html__(self):
...   return '<strong>Nice</strong>'
...
>>> escape(Foo())
'<strong>Nice</strong>'

I am using the 1.0 version I get same result for both native and speedups implementations.

>>> import markupsafe
>>> markupsafe.__version__
'1.0'
>>> from markupsafe._speedups import escape
>>> escape(Foo())
'<strong>Nice</strong>'
>>> from markupsafe._native import escape
>>> escape(Foo())
'<strong>Nice</strong>'
@dawranliou
Copy link
Contributor

Hi @mila , @davidism , I'm new to this project and would like your help.

I see the documentation for New-Style String Formatting explicitly return a Markup object instead of str object in the __html__ method. Could it be the documentation that is wrong and instead it should say the following?

>>> escape(Foo())
'<strong>Nice</strong>'
>>> Markup(Foo())
Markup(u'<strong>Nice</strong>')

@dawranliou
Copy link
Contributor

Ok I believe escape should always return the Markup object, for both str and custom class.

@mila
Copy link
Author

mila commented May 24, 2017

@dawran6 This is a very good question.

I decided to return Markup instance from my __html__ method to be safe.

On the other side I perceive the __html__ method as an interface. The Markup class is one of possible implementations. It does not seem correct to make an interface depend on one specific implementation.

@dawranliou
Copy link
Contributor

Thanks @mila for the response. I agree with you.

@davidism
Copy link
Member

fixed in #69

@davidism davidism added this to the 1.1 milestone Nov 5, 2018
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 25, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants