Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856731
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:25:17+00:00 2026-06-14T14:25:17+00:00

I am writing my doctests like this: >>> some_function(a=1, b=2) {u’id’: u’123′, u’name’: u’abc’}

  • 0

I am writing my doctests like this:

>>> some_function(a=1, b=2)
{u'id': u'123', u'name': u'abc'}

This works fine for Python version 2.5, 2.6 & 2.7 but fails for Python 3 with following error:

Expected:
    {u'id': u'123', u'name': u'abc'}
Got:
    {'id': '123', 'name': 'abc'}

Problem is that if I write my doctests like this:

>>> some_function(a=1, b=2)
{'id': '123', 'name': 'abc'}

They will work only for Python3 and fail on Python2 version. My question is how do I make it cross version compatible?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:25:19+00:00Added an answer on June 14, 2026 at 2:25 pm

    I ran into the same problem with doctests in IPython. There’s no neat solution, but I wrapped all of the u' prefixes in {}, i.e. {u}', and made a little function that would include or exclude them as appropriate.

    You can see the u_format() function and a doctest using it.

    But that’s rather messy, so I’ve moved many tests away from doctests.

    Alternatively, you can test it like this:

    >>> some_function(a=1, b=2) == {'id': '123', 'name': 'abc'}
    True
    

    If you need some unicode strings in the keys, you can use u'abþ', and use distribute to run 2to3 on the doctests. But that only works on input code, not output reprs.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Writing a python program, and I came up with this error while using the
I'm writing a python module and I would like to unit test it. I
I am currently writing a small application with Python (3.1), and like a good
Writing a shell script and I want to do something like this: cp myfile.ext
Writing some drag&drop code, I'd like to cancel the click events in my mouseup
Writing a Reporting Service (2005) report My DataSet returns something like this: DESCRIPTION COUNT
Writing floats to a CSV writes some of them like this: 2.0628800997782577e-05 c =
Writing a function I must declare input and output data types like this: int
Writing something like this using the loki library , typedef Functor<void> BitButtonPushHandler; throws a
Writing a test app to emulate PIO lines, I have a very simple Python/Tk

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.