Skip to content

backport re.compile flags default value documentation #57084

Closed

Description

@elibendersky
BPO 12875
Nosy @birkenfeld, @rhettinger, @amauryfa, @pitrou, @ezio-melotti, @merwok
Files
  • issue12875.1.patch
  • 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 = None
    closed_at = <Date 2011-11-13.22:53:25.733>
    created_at = <Date 2011-09-01.05:17:07.853>
    labels = ['docs']
    title = 'backport re.compile flags default value documentation'
    updated_at = <Date 2011-11-14.04:21:47.472>
    user = 'https://bugs.python.org/elibendersky'

    bugs.python.org fields:

    activity = <Date 2011-11-14.04:21:47.472>
    actor = 'ezio.melotti'
    assignee = 'docs@python'
    closed = True
    closed_date = <Date 2011-11-13.22:53:25.733>
    closer = 'eli.bendersky'
    components = ['Documentation']
    creation = <Date 2011-09-01.05:17:07.853>
    creator = 'eli.bendersky'
    dependencies = []
    files = ['23668']
    hgrepos = []
    issue_num = 12875
    keywords = ['patch']
    message_count = 19.0
    messages = ['143300', '143301', '143304', '143305', '143306', '143419', '147442', '147445', '147446', '147459', '147461', '147462', '147465', '147467', '147470', '147516', '147543', '147544', '147574']
    nosy_count = 9.0
    nosy_names = ['georg.brandl', 'rhettinger', 'amaury.forgeotdarc', 'pitrou', 'ezio.melotti', 'eric.araujo', 'eli.bendersky', 'docs@python', 'python-dev']
    pr_nums = []
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue12875'
    versions = ['Python 2.7']

    Activity

    1. elibendersky commented on Sep 1, 2011

      elibenderskymannequin
      MannequinAuthor

      In the 2.7 docs, re.compile has this signature:

        re.compile(pattern[, flags])

      From here it isn't clear what the default value of 'flags' is, to be able to write code like this:

        re.compile(pattern, re.I if options['ignore_case'] else <WHAT??>)

      Of course, looking at the source shows immediately that the default flag value is 0 (which is kind-of implied by the flags being a bit-OR of flags, but not mentioned explicitly).

      I saw that in the latest 3K docs, it is documented properly:

        re.compile(pattern, flags=0)

      Naturally this applies to other methods of 're' that take 'flags'.

      I hope I'm not missing something and this really is just a documentation issue. If no objections arise, I will commit a fix to the docs of 're' in 2.7

    2. ezio-melotti commented on Sep 1, 2011

      @ezio-melotti
      Member

      I don't think you are missing anything, but using ints instead of flags is discouraged (see bpo-11957). OTOH there's no re.NOFLAGS flag that can be used instead.

    3. ezio-melotti commented on Sep 1, 2011

      @ezio-melotti
      Member

      Two more things:

      1. Python 2 doc used to use the func(arg[, optional1[, optional2]]) notation, and with Python 3 we switched to func(arg, optional1=default1, optional2=default2). The latter is also used in Python 2 now, and the [] are still there in Python 3 in some places;
      2. If you are going to fix it, you can probably backport the fix from py3k with "hg diff Doc/library/re.rst -c 106ee4eb5970";
    4. elibendersky commented on Sep 1, 2011

      elibenderskymannequin
      MannequinAuthor

      Ezio,

      "but using ints instead of flags is discouraged (see bpo-11957). OTOH there's no re.NOFLAGS flag that can be used instead."

      Indeed, there's no re.NOFLAGS, and as I mentioned the 3k docs mention 0 as the default value, thus (in a way) encouraging passing the numeric value.

      Perhaps re.NOFLAGS should be added (at least in 3.3)? The docs can then be modified to specify it as the default value of 'flags' instead of 0.

    5. elibendersky commented on Sep 1, 2011

      elibenderskymannequin
      MannequinAuthor

      Regarding backporting 106ee4eb5970 - it's probably a good thing to do but I wouldn't mix it with this issue.

    6. merwok commented on Sep 2, 2011

      @merwok
      Member

      +1 to backporting [flags=0].

    7. python-dev commented on Nov 11, 2011

      python-devmannequin
      Mannequin

      New changeset 02e4d3ebbb02 by Eli Bendersky in branch '2.7':
      Issue bpo-12875: explicitly specify default value of the optional 'flags' argument to re.* functions. Closes bpo-12875
      http://hg.python.org/cpython/rev/02e4d3ebbb02

    8. amauryfa commented on Nov 11, 2011

      @amauryfa
      Contributor

      The documentation now shows::
      match(pattern, string[, flags=0])
      Is it normal to have the brackets *and* the default value?

    9. birkenfeld commented on Nov 11, 2011

      @birkenfeld
      Member

      No, it's not.

    10. elibendersky commented on Nov 12, 2011

      elibenderskymannequin
      MannequinAuthor

      Amaury & Georg,

      Grepping through the docs disagrees with your claims ;-) Try to grep for "\=None\]" to see what I mean. There are tons of places where default values are placed inside the brackets. For example in http://docs.python.org/library/csv.html --> look at:

          sniff(sample[, delimiters=None])

      or even:

      class csv.DictReader(csvfile[, fieldnames=None[, restkey=None[, restval=None[, dialect='excel'[, *args, **kwds]]]]])
      

      That said, I have absolutely no objections to following an accepted convention. But what is it?

      I looked around, and found the following in the documentation guide:

      http://docs.python.org/dev/py3k/documenting/fromlatex.html

      There is no optional command. Just give function signatures 
      like they should appear in the output:
      
      .. function:: open(filename[, mode[, buffering]])
      
         Description.
      

      This (taken from the 3.3 guide) mentions the 2.x guideline and doesn't mention default values.

      So what should I do here? According to Ezio's earlier message, the new style (without brackets) is also being used in Python 2 now. I can do the switch for the 're' module, but can we first get the convention documented somewhere?

    11. ezio-melotti commented on Nov 12, 2011

      @ezio-melotti
      Member

      but can we first get the convention documented somewhere?

      +1
      This was on my todo list, but feel free to open a new issue about it.

      Grepping through the docs disagrees with your claims

      That's because is not documented and not everyone is aware of the conventions and how/where they should be followed.

    12. elibendersky commented on Nov 12, 2011

      elibenderskymannequin
      MannequinAuthor

      Ezio, what do you suggest to do regarding *this* issue?

    13. ezio-melotti commented on Nov 12, 2011

      @ezio-melotti
      Member

      I suggest removing the [].

    14. elibendersky commented on Nov 12, 2011

      elibenderskymannequin
      MannequinAuthor

      Hmm, I've just notice that this [default=val] pattern already exists in the 're' docs in 2.7, for example:

        subn(repl, string[, count=0])

      So my change was consistent within the documentation of this module.

      No doubt, the conventions are currently a mess ;-)

      I suggest to just convert the whole 're' RST page to the new 3.x convention (i.e. model it after the same RST in the default branch). Is this acceptable? If yes, I will submit a patch for review first.

    15. ezio-melotti commented on Nov 12, 2011

      @ezio-melotti
      Member

      I created bpo-13386 about the conventions that should be followed in the doc.

      Converting the whole page is fine with me.

    16. elibendersky commented on Nov 12, 2011

      elibenderskymannequin
      MannequinAuthor

      I have converted the Doc/library/re.rst doc of 2.7 to follow the new convention of 3.x, patch attached.

    17. rhettinger commented on Nov 13, 2011

      @rhettinger
      Contributor

      This patch looks fine.

    18. ezio-melotti commented on Nov 13, 2011

      @ezio-melotti
      Member

      LGTM

    19. python-dev commented on Nov 13, 2011

      python-devmannequin
      Mannequin

      New changeset 87ecfd5cd5d1 by Eli Bendersky in branch '2.7':
      Normalize the keyword arguments documentation notation in re.rst. Closes issue bpo-12875
      http://hg.python.org/cpython/rev/87ecfd5cd5d1

    20. transferred this issue fromon Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      docsDocumentation in the Doc dir

      Projects

      No projects

        Milestone

        No milestone

        Relationships

        None yet

        Development

        No branches or pull requests

        Issue actions